import os,json,re,xlrd
from datetime import datetime
from datetime import timedelta
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.core import serializers
from django.shortcuts import render,redirect,HttpResponseRedirect
from django.http import FileResponse,HttpResponse,JsonResponse
from django.utils import timezone
from django.db.models import Count,Q
from docxtpl import DocxTemplate
from xpinyin import Pinyin
from manage_no import models
# Create your views here.
#基础View***********************************************************************************************************
@login_required(login_url='/login')
def index(request):  #首页
    return render(request, 'index.html')

@login_required(login_url='/login')
def weixin(request):  #移动业务联系人
    return render(request, 'weixin.html')

@login_required(login_url='/login')
def welcome(request):  #欢迎界面，加载办理情况的数据
    user = request.user
    if user.is_superuser==1: 
        get_message = models.message.objects.filter(relase_company='一级单位').order_by('-release_time').values()
        get_addmain_no_permission_count = models.apply_add_main.objects.filter(status='待审批').count()
        get_addvice_no_permission_count = models.apply_add_vice.objects.filter(status='待审批').count()
        get_alter_no_permission_count = models.apply_alter.objects.filter(status='待审批').count()
        get_del_permission_count = models.apply_del.objects.filter(status='待审批').count()
        no_permission_count=get_addmain_no_permission_count+get_addvice_no_permission_count+get_alter_no_permission_count+get_del_permission_count
        get_addmain_no_remake_count = models.apply_add_main.objects.filter(status='待办理').count()
        get_addvice_no_remake_count = models.apply_add_vice.objects.filter(status='待办理').count()
        get_alter_no_remake_count = models.apply_alter.objects.filter(status='待办理').count()
        get_del_remake_count = models.apply_del.objects.filter(status='待办理').count()
        no_remake_count=get_addmain_no_remake_count+get_addvice_no_remake_count+get_alter_no_remake_count+get_del_remake_count
        get_no_hz_count = models.number_main.objects.count()
        get_addmain_no_hz_count = models.apply_add_main.objects.filter(status='待回执').count()
        get_addvice_no_hz_count = models.apply_add_vice.objects.filter(status='待回执').count()
        get_alter_no_hz_count = models.apply_alter.objects.filter(status='待回执').count()
        get_del_hz_count = models.apply_del.objects.filter(status='待回执').count()
        no_hz_count=get_addmain_no_hz_count+get_addvice_no_hz_count+get_alter_no_hz_count+get_del_hz_count
        get_addmain_fail_count = models.apply_add_main.objects.filter(status='失败').count()
        get_addvice_fail_count = models.apply_add_vice.objects.filter(status='失败').count()
        get_alter_fail_count = models.apply_alter.objects.filter(status='失败').count()
        get_del_fail_count = models.apply_del.objects.filter(status='失败').count()
        no_fail_count=get_addmain_fail_count+get_addvice_fail_count+get_alter_fail_count+get_del_fail_count
        get_main_count = models.number_main.objects.count()
        get_vice_count = models.number_vice.objects.count()
    else:
        if user.is_staff==0:
            get_message = models.message.objects.filter(Q(relase_company='一级单位')|Q(relase_company=user.user_company)).order_by('-release_time').values()
            get_addmain_no_permission_count = models.apply_add_main.objects.filter(status='待审批',user_company=user.user_company).count()
            get_addvice_no_permission_count = models.apply_add_vice.objects.filter(status='待审批',id_main__user_company=user.user_company).count()
            get_alter_no_permission_count = models.apply_alter.objects.filter(status='待审批',user_company=user.user_company).count()
            get_del_permission_count = models.apply_del.objects.filter(status='待审批',user_company=user.user_company).count()
            no_permission_count=get_addmain_no_permission_count+get_addvice_no_permission_count+get_alter_no_permission_count+get_del_permission_count
            get_addmain_no_remake_count = models.apply_add_main.objects.filter(status='待办理',user_company=user.user_company).count()
            get_addvice_no_remake_count = models.apply_add_vice.objects.filter(status='待办理',id_main__user_company=user.user_company).count()
            get_alter_no_remake_count = models.apply_alter.objects.filter(status='待办理',user_company=user.user_company).count()
            get_del_remake_count = models.apply_del.objects.filter(status='待办理',user_company=user.user_company).count()
            no_remake_count=get_addmain_no_remake_count+get_addvice_no_remake_count+get_alter_no_remake_count+get_del_remake_count
            get_no_hz_count = models.number_main.objects.count()
            get_addmain_no_hz_count = models.apply_add_main.objects.filter(status='待回执',user_company=user.user_company).count()
            get_addvice_no_hz_count = models.apply_add_vice.objects.filter(status='待回执',id_main__user_company=user.user_company).count()
            get_alter_no_hz_count = models.apply_alter.objects.filter(status='待回执',user_company=user.user_company).count()
            get_del_hz_count = models.apply_del.objects.filter(status='待回执',user_company=user.user_company).count()
            no_hz_count=get_addmain_no_hz_count+get_addvice_no_hz_count+get_alter_no_hz_count+get_del_hz_count
            get_addmain_fail_count = models.apply_add_main.objects.filter(status='失败',user_company=user.user_company).count()
            get_addvice_fail_count = models.apply_add_vice.objects.filter(status='失败',id_main__user_company=user.user_company).count()
            get_alter_fail_count = models.apply_alter.objects.filter(status='失败',user_company=user.user_company).count()
            get_del_fail_count = models.apply_del.objects.filter(status='失败',user_company=user.user_company).count()
            no_fail_count=get_addmain_fail_count+get_addvice_fail_count+get_alter_fail_count+get_del_fail_count
            get_main_count = models.number_main.objects.filter(user_company=user.user_company).count()
            get_vice_count = models.number_vice.objects.filter(id_main__user_company=user.user_company).count()
        else:
            get_message = models.message.objects.filter(relase_company=user.user_company).order_by('-release_time').values()
            get_addmain_no_permission_count = models.apply_add_main.objects.filter(status='待审批',user_part=user.user_part).count()
            get_addvice_no_permission_count = models.apply_add_vice.objects.filter(status='待审批',id_main__user_part=user.user_part).count()
            get_alter_no_permission_count = models.apply_alter.objects.filter(status='待审批',user_part=user.user_part).count()
            get_del_permission_count = models.apply_del.objects.filter(status='待审批',user_part=user.user_part).count()
            no_permission_count=get_addmain_no_permission_count+get_addvice_no_permission_count+get_alter_no_permission_count+get_del_permission_count
            get_addmain_no_remake_count = models.apply_add_main.objects.filter(status='待办理',user_part=user.user_part).count()
            get_addvice_no_remake_count = models.apply_add_vice.objects.filter(status='待办理',id_main__user_part=user.user_part).count()
            get_alter_no_remake_count = models.apply_alter.objects.filter(status='待办理',user_part=user.user_part).count()
            get_del_remake_count = models.apply_del.objects.filter(status='待办理',user_part=user.user_part).count()
            no_remake_count=get_addmain_no_remake_count+get_addvice_no_remake_count+get_alter_no_remake_count+get_del_remake_count
            get_no_hz_count = models.number_main.objects.count()
            get_addmain_no_hz_count = models.apply_add_main.objects.filter(status='待回执',user_part=user.user_part).count()
            get_addvice_no_hz_count = models.apply_add_vice.objects.filter(status='待回执',id_main__user_part=user.user_part).count()
            get_alter_no_hz_count = models.apply_alter.objects.filter(status='待回执',user_part=user.user_part).count()
            get_del_hz_count = models.apply_del.objects.filter(status='待回执',user_part=user.user_part).count()
            no_hz_count=get_addmain_no_hz_count+get_addvice_no_hz_count+get_alter_no_hz_count+get_del_hz_count
            get_addmain_fail_count = models.apply_add_main.objects.filter(status='失败',user_part=user.user_part).count()
            get_addvice_fail_count = models.apply_add_vice.objects.filter(status='失败',id_main__user_part=user.user_part).count()
            get_alter_fail_count = models.apply_alter.objects.filter(status='失败',user_part=user.user_part).count()
            get_del_fail_count = models.apply_del.objects.filter(status='失败',user_part=user.user_part).count()
            no_fail_count=get_addmain_fail_count+get_addvice_fail_count+get_alter_fail_count+get_del_fail_count
            get_main_count = models.number_main.objects.filter(user_part=user.user_part).count()
            get_vice_count = models.number_vice.objects.filter(id_main__user_part=user.user_part).count()
    get_user_part_list = models.userext.objects.filter(id=user.id).values("user_part")
    get_user_part_dict = get_user_part_list[0]
    get_part_list = models.part.objects.filter(id=get_user_part_dict["user_part"]).values("part_name")
    get_part_dict = get_part_list[0]
    context = {
        'get_message': get_message,
        'no_permission_count':no_permission_count,
        'no_remake_count':no_remake_count,
        'no_hz_count':no_hz_count,
        'main_count':get_main_count,
        'vice_count':get_vice_count,
        'no_fail_count':no_fail_count,
        'user_part':get_part_dict["part_name"],
    }
    return render(request, 'welcome.html', context)

def user_login(request): #用户登录
    if request.method == 'POST':
        input_username = request.POST['name']
        input_pwd = request.POST['pwd']
        user = authenticate(username=input_username,password=input_pwd)
        if user is not None:
            login(request,user)
            return redirect('/index')
        else:
            login_err = '用户名或密码错误'
            return  render(request,'login.html',{'login_err':login_err})
    return render(request,'login.html')

def user_logout(request): #用户登出
    logout(request)
    return HttpResponseRedirect('/')

def search(request): #信息查询，过滤不希望被查询的用户
    if request.method == 'POST':
        try:
            keyword_t = request.POST['keyword_t']
            keyword_r = request.POST['keyword_r']
            page = request.POST['page']
            limit = request.POST['limit']
            i_id=0
            if keyword_r=='user_name':
                get_data1 = models.number_main.objects.filter(user_name=keyword_t,is_app=1).values('user_name','phone_number','short_number')
                get_data2 = models.number_vice.objects.filter(user_name=keyword_t).values('user_name','phone_number','short_number')
                get_data=[]
                for i in get_data1:
                    get_data.append(i)
                for i in get_data2:
                    get_data.append(i)
            elif keyword_r=='phone_number':
                get_data1 = models.number_main.objects.filter(phone_number=keyword_t,is_app=1).values('user_name','phone_number','short_number')
                get_data2 = models.number_vice.objects.filter(phone_number=keyword_t).values('user_name','phone_number','short_number')
                get_data=[]
                for i in get_data1:
                    get_data.append(i)
                for i in get_data2:
                    get_data.append(i)
            elif keyword_r=='short_number':
                get_data1 = models.number_main.objects.filter(short_number=keyword_t,is_app=1).values('user_name','phone_number','short_number')
                get_data2 = models.number_vice.objects.filter(short_number=keyword_t).values('user_name','phone_number','short_number')
                get_data=[]
                for i in get_data1:
                    get_data.append(i)
                for i in get_data2:
                    get_data.append(i)
            count=len(get_data)
            paginator = Paginator(get_data, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
        except:
            data={"code":0,"msg":"","count":0,"data":''}
            return JsonResponse(data,safe=False)
    else:
        return render(request, "search.html")

def staff_only(function):#二级单位权限限制
    def _inner(request,*args,**kwargs):
        if  request.user.is_staff:
            return render(request, '404.html')
        return function(request,*args,**kwargs)
    return _inner 

def superuser_only(function):#超级管理员权限限制
    def _inner(request,*args,**kwargs):
        if not request.user.is_superuser:
            return render(request, '404.html')
        return function(request,*args,**kwargs)
    return _inner 
#基础View End***********************************************************************************************************
#图表url ***********************************************************************************************************
@login_required(login_url='/login')
def chart_part(request): #图表展示
    content_list=[]
    data_part = []
    data_part_f = []
    data_part_count_z = []
    data_part_count_f = []
    user = request.user
    if user.is_superuser==1:
        get_list_z = models.number_main.objects.values('user_company').annotate(part_count = Count('id')).order_by('part_count')
        get_list_f = models.number_vice.objects.values('id_main__user_company').annotate(part_count = Count('id'))
        for i in get_list_z:
            data_part.append(i.get('user_company'))
            data_part_count_z.append(i.get('part_count'))
            for l  in get_list_f:
                if i['user_company']==l['id_main__user_company']:
                    data_part_count_f.append(l.get('part_count'))
    else:
        get_list_z = models.number_main.objects.filter(user_company=user.user_company).values('user_part__part_name').annotate(part_count = Count('id')).order_by('part_count')
        get_list_f = models.number_vice.objects.filter(id_main__user_company=user.user_company).values('id_main__user_part__part_name').annotate(part_count = Count('id'))
        for i in get_list_z:
            if i.get('user_part__part_name') is None:
                data_part.append('员工类别3')
            else:
                data_part.append(i.get('user_part__part_name'))
            data_part_count_z.append(i.get('part_count'))
            is_in=1
            for l  in get_list_f:
                if i['user_part__part_name']==l['id_main__user_part__part_name']:
                    data_part_count_f.append(l.get('part_count'))
                    is_in=1
                    break
                else:
                    is_in=0
            if is_in==0:
                data_part_count_f.append(0)
    content_list=[{'name':'主号','type':'bar','data':data_part_count_z},{'name':'副号','type':'bar','data':data_part_count_f}]
    context = {
        'ContentInfo': content_list,
        'data': data_part,
    }
    return render(request, "chart/part_chart.html",context)

@login_required(login_url='/login')
def chart_identity(request): #图表展示
    data_identity = []
    data_identity_count = []
    user = request.user
    if user.is_superuser==1:
        get_list_z = models.number_main.objects.values('user_identity').annotate(part_count = Count('id')).order_by('part_count')
        get_list_f = models.number_vice.objects.values('id_main__user_identity').annotate(part_count = Count('id'))
        for i in get_list_z:
            data_identity.append(i.get('user_identity'))
            line = {'value': i.get('part_count'),'name': i.get('user_identity')}
            data_identity_count.append(line)
        for l in get_list_f:
            user_identity=l.get('id_main__user_identity')+'家属'
            data_identity.append(user_identity)
            line = {'value': l.get('part_count'), 'name': user_identity}
            data_identity_count.append(line)
    else:
        get_list_z = models.number_main.objects.filter(user_company=user.user_company).values('user_identity').annotate(part_count = Count('id')).order_by('part_count')
        get_list_f = models.number_vice.objects.filter(id_main__user_company=user.user_company).values('id_main__user_identity').annotate(part_count = Count('id'))
        for i in get_list_z:
            data_identity.append(i.get('user_identity'))
            line = {'value': i.get('part_count'),'name': i.get('user_identity')}
            data_identity_count.append(line)
        for l in get_list_f:
            user_identity=l.get('id_main__user_identity')+'家属'
            data_identity.append(user_identity)
            line = {'value': l.get('part_count'), 'name': user_identity}
            data_identity_count.append(line)
    content_list=[{'name':'类别统计','type':'pie','radius':'55%','center': ['50%', '60%'],'data':data_identity_count,'emphasis': {'itemStyle': {'shadowBlur': 10,'shadowOffsetX': 0,'shadowColor': 'rgba(0, 0, 0, 0.5)' }}}]
    context = {
        'content_list': content_list,
        'data_identity': data_identity,
    }
    return render(request, "chart/identit_chart.html",context)
#图表url End***********************************************************************************************************
#系统管理View **********************************************************************************************************
@staff_only
@login_required(login_url='/login')
def add_user(request): #添加用户
    user = request.user
    if request.POST:
        username = request.POST['username']
        try:
            u_name=models.userext.objects(username=username)
            if u_name is not None:
                return HttpResponse('{"status":"fail","msg":"用户名重复"}',content_type='application/json')
        except:
            pwd = request.POST['pass']
            phone = request.POST['phone']
            ssjg = request.POST['ssjg']
            ssbm = request.POST['ssbm']
            is_manage = request.POST['is_manage']
            part_id = models.part.objects.get(id=ssbm)
            if is_manage=='2':
                user = models.userext.objects.create_user(username=username, password=pwd,email=phone,user_part=part_id,user_company=ssjg,is_staff='1')
            elif is_manage=='1':
                user = models.userext.objects.create_user(username=username, password=pwd,email=phone,user_part=part_id,user_company=ssjg,is_superuser='1')
            elif is_manage=='0':
                user = models.userext.objects.create_user(username=username, password=pwd,email=phone,user_part=part_id,user_company=ssjg)
            user.save()
            return HttpResponse('{"status":"success","msg":"添加成功"}',content_type='application/json')
    else:
        part_list = models.part.objects.filter(company_name=user.user_company).values()
        context = {
        'part_list': part_list,
            }
        return render(request, "manage_user/add_user.html", context)

@login_required(login_url='/login')
def alter_user_pwd(request):#修改密码
    if request.method == 'POST':
        input_pwd = request.POST['pass']
        input_old_pwd = request.POST['old_pass']
        user = authenticate(username=request.user,password=input_old_pwd)
        if user is not None:
            user.set_password(input_pwd)
            user.save()
            return HttpResponse('{"status":"success","msg":"修改成功"}',content_type='application/json')
        else:
            return HttpResponse('{"status":"fail","msg":"旧密码错误"}',content_type='application/json')
    else:
        return render(request, "alter_pwd.html")

@staff_only
@login_required(login_url='/login')
def user_info(request): #用户信息管理
    user = request.user
    if user.is_superuser==1:
        content_list = models.userext.objects.filter().values('id','username','user_company','user_part__part_name','email','last_login','date_joined','is_superuser','is_staff')
        for i in content_list:
            if i['is_superuser']==1:
                qx='超级管理员'
            else:
                if i['is_staff']==0:
                    qx='二级单位管理员'
                else:
                    qx='用户'
            i['qx'] = qx
        context = {
            'ContentInfo': content_list,
        }
        return render(request, 'manage_user/user_info.html', context)
    else:
        content_list = models.userext.objects.filter(user_company=user.user_company,is_superuser=0).values('id','username','user_company','user_part__part_name','email','last_login','date_joined','is_superuser','is_staff')
        for i in content_list:
            if i['is_superuser']==1:
                qx='超级管理员'
            else:
                if i['is_staff']==0:
                    qx='旗县管理员'
                else:
                    qx='用户'
            i['qx'] = qx
        context = {
            'ContentInfo': content_list,
        }
        return render(request, 'manage_user/user_info.html', context)

@staff_only
@login_required(login_url='/login')
def del_user(request,post_id):#删除用户
    if ',' in post_id:
        try:
            id_list=post_id.split(',')
            for i in id_list:
                 models.userext.objects.filter(id=i).delete()
            return HttpResponse('{"status":"success","msg":"删除成功"}',content_type='application/json')
        except:
            return HttpResponse('{"status":"fail","msg":"删除失败"}',content_type='application/json')
    else:
        try:
            models.userext.objects.filter(id=post_id).delete()
            return HttpResponse('{"status":"success","msg":"删除成功"}',content_type='application/json')
        except:
            return HttpResponse('{"status":"fail","msg":"删除失败"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def alter_user(request,post_id):#修改用户信息
    content_list = models.userext.objects.filter(id=post_id).values('id','username','user_company','user_part__part_name','email','last_login','date_joined','is_superuser','is_staff')
    content = content_list[0]
    if content['is_superuser']==1:
        qx='超级管理员'
    else:
        if content['is_staff']==0:
            qx='二级单位管理员'
        else:
            qx='用户'
    content['qx'] = qx
    context = {
        'content': content,
    }
    return render(request, 'manage_user/alter_user.html', context)

@staff_only
@login_required(login_url='/login')
def save_alter_user(request):#保存修改用户信息信息
    username = request.POST['user_name']
    phone = request.POST['phone']
    models.userext.objects.filter(username=username).update(email=phone)
    models.userext.objects.filter(username=username).update(password='pbkdf2_sha256$100000$3I61dM8KTpIX$CiVyXxUQNGTRnHmlpSkdVneoKUHa+HUqHf1TCH5wFDw=')
    return HttpResponse('{"status":"success","msg":"修改成功"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def message_info(request):#通知列表
    user = request.user
    get_message = models.message.objects.filter(relase_user=request.user).values()
    paginator = Paginator(get_message, 15)
    page = request.GET.get('page', 1)
    loaded = paginator.page(page)
    context = {
        'ContentInfo': loaded,
    }
    return render(request, 'manage_user/message_info.html', context)

@staff_only
@login_required(login_url='/login')
def add_message(request):#添加通知
    if request.POST:
        try:
            user = request.user
            title = request.POST['title']
            desc = request.POST['desc']
            models.message.objects.create(title=title,content=desc,relase_user=user.username,relase_company=user.user_company)
            return HttpResponse('{"status":"success","msg":"添加成功"}',content_type='application/json')
        except expression as identifier:
            return HttpResponse('{"status":"fail","msg":"添加失败"}',content_type='application/json')
    else:
        return render(request, "manage_user/add_message.html")

@staff_only
@login_required(login_url='/login')
def del_message(request,post_id):#删除通知
    if ',' in post_id:
        try:
            id_list=post_id.split(',')
            for i in id_list:
                models.message.objects.filter(id=i).delete()
            return HttpResponse('{"status":"success","msg":"删除成功"}',content_type='application/json')
        except:
            return HttpResponse('{"status":"fail","msg":"删除失败"}',content_type='application/json')
    else:
        try:
            models.message.objects.filter(id=post_id).delete()
            return HttpResponse('{"status":"success","msg":"删除成功"}',content_type='application/json')
        except:
            return HttpResponse('{"status":"fail","msg":"删除失败"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def alter_message(request,post_id):#修改通知
    content = models.message.objects.get(id=post_id)
    context = {
        'content': content,
    }
    return render(request, 'manage_user/alter_message.html', context)

@staff_only
@login_required(login_url='/login')
def save_alter_message(request):#保存修改通知
    id_ms = request.POST['id_ms']
    title = request.POST['title']
    desc = request.POST['desc']
    models.message.objects.filter(id=id_ms).update(title=title,content=desc)
    return HttpResponse('{"status":"success","msg":"修改成功"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def part_info(request):#部门列表
    user = request.user
    if user.is_superuser==1:
        get_part = models.part.objects.filter().values()
    else:
        get_part = models.part.objects.filter(company_name=user.user_company).values()
    context = {
        'ContentInfo': get_part,
    }
    return render(request, 'manage_user/part_info.html', context)

@staff_only
@login_required(login_url='/login')
def add_part(request):#添加部门
    user = request.user
    if request.POST:
        try:
            part = request.POST['part']
            company = request.POST['company']
            models.part.objects.create(part_name=part,company_name=user.user_company)
            return HttpResponse('{"status":"success","msg":"添加成功"}',content_type='application/json')
        except:
            return HttpResponse('{"status":"fail","msg":"添加失败"}',content_type='application/json')
    else:
        context = {
        'user_company': user.user_company,
             }
        return render(request, "manage_user/add_part.html",context)

@staff_only
@login_required(login_url='/login')
def del_part(request,post_id):#删除部门
    if ',' in post_id:
        try:
            id_list=post_id.split(',')
            for i in id_list:
                models.part.objects.filter(id=i).delete()
            return HttpResponse('{"status":"success","msg":"删除成功"}',content_type='application/json')
        except:
            return HttpResponse('{"status":"fail","msg":"删除失败"}',content_type='application/json')
    else:
        try:
            models.part.objects.filter(id=post_id).delete()
            return HttpResponse('{"status":"success","msg":"删除成功"}',content_type='application/json')
        except:
            return HttpResponse('{"status":"fail","msg":"删除失败"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def alter_part(request,post_id):#修改部门
    content = models.part.objects.get(id=post_id)
    context = {
        'content': content,
    }
    return render(request, 'manage_user/alter_part.html', context)

@staff_only
@login_required(login_url='/login')
def save_alter_part(request):#保存修改部门
    id_pt = request.POST['id_ms']
    part = request.POST['part']
    models.part.objects.filter(id=id_pt).update(part_name=part)
    return HttpResponse('{"status":"success","msg":"修改成功"}',content_type='application/json')

#系统管理View End*********************************************************************************************************
#信息维护Views*********************************************************************************************************
@staff_only
@login_required(login_url='/login')
def main_manage(request):#主号信息维护
    return render(request, 'manage_no/main/main_manage.html')

@staff_only
@login_required(login_url='/login')
def main_manage_del(request,post_id):#主号信息维护删除 
    try:
        models.number_main.objects.filter(id=post_id).delete()
        return HttpResponse('{"status":"success","msg":"删除成功"}',content_type='application/json')
    except:
        return HttpResponse('{"status":"fail","msg":"删除失败"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def main_manage_alter(request):#主号信息维护编辑
    value = request.POST['value']
    field = request.POST['field']
    main_id = request.POST['id']
    if field == 'user_name':
        models.number_main.objects.filter(id=main_id).update(user_name=value)
    elif field == 'id_card':
        models.number_main.objects.filter(id=main_id).update(id_card=value)
    elif field == 'id_work':
        models.number_main.objects.filter(id=main_id).update(id_work=value)
    elif field == 'phone_number':
        models.number_main.objects.filter(id=main_id).update(phone_number=value)
    elif field == 'short_number':
        get_num_list = models.number_main.objects.filter(id=main_id).values()
        get_num_dict= get_num_list[0]
        models.no_pool.objects.filter(short_number=get_num_dict['short_number']).update(is_use='0')
        models.number_main.objects.filter(id=main_id).update(short_number=value)
        models.no_pool.objects.filter(short_number=value).update(is_use='1')
    return HttpResponse('{"status":"success","msg":"修改成功"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def main_manage_reg(request):#添加新增申请
    user = request.user
    part_list = models.part.objects.filter(company_name=user.user_company).values()
    context = {
        'part_list': part_list,
    }
    return render(request, "manage_no/main/apply_reg_main.html",context)

@staff_only
@login_required(login_url='/login')
def save_reg_main(request):#添加新增申请
    user_name = request.POST['user_name']
    id_card = request.POST['id_card']
    phone_number = request.POST['phone_number']
    short_number = request.POST['short_number']  
    user_company = request.POST['user_company']
    user_identity = request.POST['user_identity']  
    remark = request.POST['remark']
    if user_identity=='员工类别3':
        models.number_main.objects.create(user_name=user_name,id_card=id_card,user_company=user_company,remark=remark,user_identity=user_identity,phone_number=phone_number,short_number=short_number)
    else:
        id_work = request.POST['id_work']
        user_part = request.POST['user_part']   
        user_position = request.POST['user_position'] 
        models.number_main.objects.create(user_name=user_name,id_card=id_card,id_work=id_work,user_company=user_company,remark=remark,
        user_identity=user_identity,user_part_id=user_part,user_position=user_position,phone_number=phone_number,short_number=short_number)
    models.no_pool.objects.filter(short_number=short_number).update(is_use='1')
    return HttpResponse('{"status":"success","msg":"添加成功"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def vice_manage(request):#副号信息维护
    return render(request, 'manage_no/vice/vice_manage.html')

@staff_only
@login_required(login_url='/login')
def vice_manage_del(request,post_id):#副号信息维护删除 
    try:
        models.number_vice.objects.filter(id=post_id).delete()
        return HttpResponse('{"status":"success","msg":"删除成功"}',content_type='application/json')
    except:
        return HttpResponse('{"status":"fail","msg":"删除失败"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def vice_manage_alter(request):#副号信息维护编辑
    value = request.POST['value']
    field = request.POST['field']
    main_id = request.POST['id']
    if field == 'user_name':
        models.number_vice.objects.filter(id=main_id).update(user_name=value)
    elif field == 'id_card':
        models.number_vice.objects.filter(id=main_id).update(id_card=value)
    elif field == 'phone_number':
        models.number_vice.objects.filter(id=main_id).update(phone_number=value)
    elif field == 'short_number':
        get_num_list = models.number_vice.objects.filter(id=main_id).values()
        get_num_dict= get_num_list[0]
        models.no_pool.objects.filter(short_number=get_num_dict['short_number']).update(is_use='0')
        models.number_vice.objects.filter(id=main_id).update(short_number=value)
        models.no_pool.objects.filter(short_number=value).update(is_use='1')
    return HttpResponse('{"status":"success","msg":"修改成功"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def vice_manage_reg(request):#添加新增申请
    user = request.user
    return render(request, "manage_no/vice/apply_reg_vice.html")

@staff_only
@login_required(login_url='/login')
def save_reg_vice(request):#添加新增申请
    try:
        id_main = request.POST['user_main']
        user_name = request.POST['user_name']
        id_card = request.POST['id_card']
        phone_number = request.POST['phone_number']
        short_number = request.POST['short_number']  
        remark = request.POST['remark']
        models.no_pool.objects.filter(short_number=short_number).update(is_use='1')
        obj = models.number_main.objects.get(id=id_main)
        models.number_vice.objects.create(id_main=obj,user_name=user_name,id_card=id_card,remark=remark,phone_number=phone_number,short_number=short_number)
        return HttpResponse('{"status":"success","msg":"添加成功"}',content_type='application/json')
    except:
        return HttpResponse('{"status":"fail","msg":"添加失败"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def error_log(request):#删除申请日志
    return render(request, 'manage_no/error_log.html')

@staff_only
@login_required(login_url='/login')
def error_logdata(request):#删除申请数据
    user = request.user
    if user.is_superuser==1:
        page = request.POST['page']
        limit = request.POST['limit']
        get_num_list = models.number_error.objects.filter().values().order_by('error_time')
        count=len(get_num_list)
        paginator = Paginator(get_num_list, limit)
        loaded = paginator.page(page)
        loaded = list(loaded)
        for i in loaded:
            if i['number_type']=='main':
                i['number_type']='主号'
            else:
                i['number_type']='副号'
        data={"code":0,"msg":"","count":count,"data":loaded}
        return JsonResponse(data,safe=False)
    else:
        page = request.POST['page']
        limit = request.POST['limit']
        get_num_list = models.number_error.objects.filter(user_company=user.user_company).values().order_by('error_time')
        count=len(get_num_list)
        paginator = Paginator(get_num_list, limit)
        loaded = paginator.page(page)
        loaded = list(loaded)
        data={"code":0,"msg":"","count":count,"data":loaded}
        for i in loaded:
            if i['number_type']=='main':
                i['number_type']='主号'
            else:
                i['number_type']='副号'
        return JsonResponse(data,safe=False)

@staff_only
@login_required(login_url='/login')
def short_manage(request):#主号信息维护
    return render(request, 'manage_no/short_manage.html')

@staff_only
@login_required(login_url='/login')
def short_manage_data(request):#主号信息维护
    user = request.user
    i_id=0
    if user.is_superuser==1:
        try:
            keyword_t = request.POST['keyword_t']
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.no_pool.objects.filter(short_number__icontains=keyword_t).values().order_by('short_number')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id                    
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
        except:
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.no_pool.objects.filter().values().order_by('short_number')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id                
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
    else:
        try:
            keyword_t = request.POST['keyword_t']
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.no_pool.objects.filter(short_number__icontains=keyword_t,belong=user.user_company).values().order_by('short_number')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id                  
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
        except:
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.no_pool.objects.filter(belong=user.user_company).values().order_by('short_number')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)

@staff_only
@login_required(login_url='/login')
def app_manage(request):#主号信息维护
    return render(request, 'manage_no/app_manage.html')

@staff_only
@login_required(login_url='/login')
def app_manage_data(request):#主号信息维护
    user = request.user
    i_id=0
    if user.is_superuser==1:
        try:
            keyword_t = request.POST['keyword_t']
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.number_main.objects.filter(short_number__icontains=keyword_t).values().order_by('short_number')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id                    
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
        except:
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.number_main.objects.filter().values().order_by('short_number')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id                
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
    else:
        try:
            keyword_t = request.POST['keyword_t']
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.number_main.objects.filter(short_number__icontains=keyword_t,user_company=user.user_company).values().order_by('short_number')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id                  
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
        except:
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.number_main.objects.filter(user_company=user.user_company).values().order_by('short_number')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)

@staff_only
@login_required(login_url='/login')
def save_alter_short(request):#保存修改部门
    short_value = request.POST['short_id']
    is_type = request.POST['is_type']
    short_value_list=short_value.split('_')
    short_id = short_value_list[0]
    short_status = short_value_list[1]
    if is_type=='is_re':
        if short_status=='1':
            models.no_pool.objects.filter(id=short_id).update(is_re=0)
        else:
            models.no_pool.objects.filter(id=short_id).update(is_re=1)
    else:
        if short_status=='1':
            models.no_pool.objects.filter(id=short_id).update(is_use=0)
        else:
            models.no_pool.objects.filter(id=short_id).update(is_use=1)
    return HttpResponse('{"status":"success","msg":"修改成功"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def save_alter_app(request):#保存修改部门
    short_value = request.POST['short_id']
    short_value_list=short_value.split('_')
    short_id = short_value_list[0]
    short_status = short_value_list[1]
    if short_status=='1':
        models.number_main.objects.filter(id=short_id).update(is_app=0)
    else:
        models.number_main.objects.filter(id=short_id).update(is_app=1)
    return HttpResponse('{"status":"success","msg":"修改成功"}',content_type='application/json')
#信息维护Views End**************************************************************************************************
#基础函数*********************************************************************************************************
@login_required(login_url='/login')
def get_part(request):#获取部门
    company_name = request.POST['company_name']
    part_list = models.part.objects.filter(company_name=company_name)
    data = serializers.serialize('json',part_list)
    return HttpResponse(data)

@login_required(login_url='/login')
def get_user_main(request):#获取部门内的人员
    part_id = request.POST['part_id']
    part_list = models.number_main.objects.filter(user_part_id=part_id)
    data = serializers.serialize('json',part_list)
    return HttpResponse(data)

def del_type(del_type_id):#判断删除类型
    if del_type_id=='1':
        del_type_str='全部删除'
    elif del_type_id=='2':
        del_type_str='仅删除主号'
    elif del_type_id=='3':
        del_type_str='仅删除副号'
    return del_type_str

@login_required(login_url='/login')
def apply_alter_short(request):#办理变更小号时请求小号列表
    belong = request.POST['belong']
    user = request.user
    if user.is_superuser==1:
        short_num_list = models.no_pool.objects.filter(is_use='0').order_by('short_number')
        data = serializers.serialize('json',short_num_list)
    else:
        short_num_list = models.no_pool.objects.filter(belong=belong,is_use='0').order_by('short_number')
        data = serializers.serialize('json',short_num_list)
    return HttpResponse(data)

@login_required(login_url='/login')
def apply_no_repeat(request):#办理申请验证重复申请
    phone_number = request.POST['phone_number']
    is_exist_main = models.number_main.objects.filter(phone_number=phone_number).values()#验证是否在库主号码
    is_exist_vice = models.number_vice.objects.filter(phone_number=phone_number).values()#验证是否在库家属号码
    is_add_main = models.apply_add_main.objects.filter(phone_number=phone_number).values().order_by('-status_time')#验证是否在已申请的主号码
    is_add_vice = models.apply_add_vice.objects.filter(phone_number=phone_number).values().order_by('-status_time')#验证是否在已申请的主号码
    is_alter = models.apply_alter.objects.filter(phone_number=phone_number).values().order_by('-status_time')#验证是否在已申请的主号码
    is_del = models.apply_del.objects.filter(phone_number=phone_number).values().order_by('-status_time')#验证是否在已申请的主号码
    if len(is_exist_main)>0:
        return HttpResponse('{"status":"fail","fail_msg":"该手机号码已有集团短号。"}',content_type='application/json')
    elif len(is_exist_vice)>0:
        return HttpResponse('{"status":"fail","fail_msg":"该手机号码已有集团短号。"}',content_type='application/json')
    elif len(is_add_main)>0:
        is_apply = is_add_main[0]
        status = is_apply['status']
        if  status =='已完成':
            pass
        elif status =='失败':
            pass
        else:
            return HttpResponse('{"status":"fail","fail_msg":"该手机号码已申请集团短号。"}',content_type='application/json')
    elif len(is_add_vice)>0:
        is_apply = is_add_vice[0]
        status = is_apply['status']
        if  status =='已完成':
            pass
        elif status =='失败':
            pass
        else:
            return HttpResponse('{"status":"fail","fail_msg":"该手机号码已申请集团短号。"}',content_type='application/json')
    elif len(is_alter)>0:
        is_apply = is_alter[0]
        status = is_apply['status']
        if  status =='已完成':
            pass
        elif status =='失败':
            pass
        else:
            return HttpResponse('{"status":"fail","fail_msg":"该手机号码已申请变更。"}',content_type='application/json')
    elif len(is_del)>0:
        is_apply = is_del[0]
        status = is_apply['status']
        if  status =='已完成':
            pass
        elif status =='失败':
            pass
        else:
            return HttpResponse('{"status":"fail","fail_msg":"该手机号码已申请删除。"}',content_type='application/json')
    else:
        return HttpResponse('{"status":"success"',content_type='application/json')

@login_required(login_url='/login')
def apply_alter_repeat(request):#办理申请验证重复申请
    number_id = request.POST['number_id']
    number_type = request.POST['number_type']
    is_alter = models.apply_alter.objects.filter(number_id=number_id,number_type=number_type).values().order_by('-status_time')#验证是否在已申请的主号码
    is_del = models.apply_del.objects.filter(number_id=number_id,number_type=number_type).values().order_by('-status_time')#验证是否在已申请的主号码
    if  len(is_alter)>0:
        is_apply = is_alter[0]
        status = is_apply['status']
        if  status =='已完成':
            pass
        elif status =='失败':
            pass
        else:
            return HttpResponse('{"status":"fail","fail_msg":"该手机号码已申请变更。"}',content_type='application/json')
    elif len(is_del)>0:
        is_apply = is_del[0]
        status = is_apply['status']
        if  status =='已完成':
            pass
        elif status =='失败':
            pass
        else:
            return HttpResponse('{"status":"fail","fail_msg":"该手机号码已申请删除。"}',content_type='application/json')
    else:
        return HttpResponse('{"status":"success"',content_type='application/json')

@login_required(login_url='/login')
def apply_short_repeat(request):#办理申请验证重复申请
    short_number = request.POST['short_number']
    if short_number =='请选择' or short_number =='Null' :
        return HttpResponse('{"status":"fail","fail_msg":"请选择号码"}',content_type='application/json') 
    is_use_list = models.no_pool.objects.filter(short_number=short_number).values('is_use')#验证是否在已申请的主号码
    is_use = is_use_list[0]
    if is_use['is_use']==True:
        return HttpResponse('{"status":"fail","fail_msg":"请选择可用号码"}',content_type='application/json') 
    else:
        return HttpResponse('{"status":"success"',content_type='application/json')

# 是否
#基础函数 End*********************************************************************************************************

#主号View *********************************************************************************************************
@login_required(login_url='/login')
def main_info(request):#主号列表
    return render(request, 'manage_no/main/main_info.html')

@login_required(login_url='/login')
def main_data(request):#主号列表数据
    user = request.user
    i_id=0
    if user.is_superuser==1:
        try:
            keyword_t = request.POST['keyword_t']
            keyword_r = request.POST['keyword_r']
            page = request.POST['page']
            limit = request.POST['limit']
            if keyword_r=='user_name':
                get_num_list = models.number_main.objects.filter(user_name__icontains=keyword_t).values('id','user_name','id_card','id_work'
                ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='phone_number':
                get_num_list = models.number_main.objects.filter(phone_number__icontains=keyword_t).values('id','user_name','id_card','id_work'
                ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id                    
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='short_number':
                get_num_list = models.number_main.objects.filter(short_number__icontains=keyword_t).values('id','user_name','id_card','id_work'
                ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id                    
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='id':
                get_num_list = models.number_main.objects.filter(id=keyword_t).values('id','user_name','id_card','id_work'
                ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id                    
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
        except:
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.number_main.objects.filter().values('id','user_name','id_card','id_work'
            ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id                
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
    else:
        if user.is_staff==0:
            try:
                keyword_t = request.POST['keyword_t']
                keyword_r = request.POST['keyword_r']
                page = request.POST['page']
                limit = request.POST['limit']
                if keyword_r=='user_name':
                    get_num_list = models.number_main.objects.filter(user_name__icontains=keyword_t,user_company=user.user_company).values('id','user_name','id_card','id_work'
                    ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id                        
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='phone_number':
                    get_num_list = models.number_main.objects.filter(phone_number__icontains=keyword_t,user_company=user.user_company).values('id','user_name','id_card','id_work'
                    ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id                 
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='short_number':
                    get_num_list = models.number_main.objects.filter(short_number__icontains=keyword_t,user_company=user.user_company).values('id','user_name','id_card','id_work'
                    ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id                  
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='id':
                    get_num_list = models.number_main.objects.filter(id=keyword_t,user_company=user.user_company).values('id','user_name','id_card','id_work'
                    ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id                  
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
            except:
                page = request.POST['page']
                limit = request.POST['limit']
                get_num_list = models.number_main.objects.filter(user_company=user.user_company).values('id','user_name','id_card','id_work'
                ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
        else:
            try:
                keyword_t = request.POST['keyword_t']
                keyword_r = request.POST['keyword_r']
                page = request.POST['page']
                limit = request.POST['limit']
                if keyword_r=='user_name':
                    get_num_list = models.number_main.objects.filter(user_name__icontains=keyword_t,user_company=user.user_company,user_part=user.user_part).values('id','user_name','id_card','id_work'
                    ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id                
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='phone_number':
                    get_num_list = models.number_main.objects.filter(phone_number__icontains=keyword_t,user_company=user.user_company,user_part=user.user_part).values('id','user_name','id_card','id_work'
                    ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id                
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='short_number':
                    get_num_list = models.number_main.objects.filter(short_number__icontains=keyword_t,user_company=user.user_company,user_part=user.user_part).values('id','user_name','id_card','id_work'
                    ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id            
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
            except:
                page = request.POST['page']
                limit = request.POST['limit']
                get_num_list = models.number_main.objects.filter(user_part=user.user_part).values('id','user_name','id_card','id_work'
                ,'user_company','user_part__part_name','user_identity','user_position','phone_number','short_number').order_by('short_number')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)

@login_required(login_url='/login')
def add_main(request):#添加新增申请
    user = request.user
    part_list = models.part.objects.filter(company_name=user.user_company).values()
    context = {
        'part_list': part_list,
    }
    return render(request, "manage_no/main/apply_add_main.html",context)

@login_required(login_url='/login')
def save_add_main(request):#添加新增申请
    user_name = request.POST['user_name']
    id_card = request.POST['id_card']
    phone_number = request.POST['phone_number']
    user_company = request.POST['user_company']
    user_identity = request.POST['user_identity']  
    remark = request.POST['remark']
    if user_identity=='员工类别3':
        models.apply_add_main.objects.create(user_name=user_name,id_card=id_card,user_company=user_company,remark=remark,
        status='待审批',user_identity=user_identity,phone_number=phone_number,apply_user=request.user,status_time=datetime.now())
    else:
        id_work = request.POST['id_work']
        user_part = request.POST['user_part']   
        user_position = request.POST['user_position'] 
        models.apply_add_main.objects.create(user_name=user_name,id_card=id_card,id_work=id_work,user_company=user_company,status='待审批',remark=remark,
        user_identity=user_identity,user_part_id=user_part,user_position=user_position,phone_number=phone_number,apply_user=request.user,status_time=datetime.now())
    return HttpResponse('{"status":"success","msg":"添加成功"}',content_type='application/json')

@login_required(login_url='/login')
def add_log_main(request):#主号新增日志
    return render(request, 'manage_no/main/add_log_main.html')

@login_required(login_url='/login')
def add_log_data_main(request):#新增主号日志数据
    user = request.user
    i_id=0
    if user.is_superuser==1:
        try:
            keyword_t = request.POST['keyword_t']
            keyword_r = request.POST['keyword_r']
            page = request.POST['page']
            limit = request.POST['limit']
            if keyword_r=='user_name':
                get_data = models.apply_add_main.objects.filter(user_name__icontains=keyword_t).values('id','user_company','user_part__part_name','user_name',
                'user_identity','id_card','id_work','user_position','phone_number','short_number','status').order_by('status_time')
                all_data=[]
                for i in get_data:
                    i_id=i_id+1
                    i['i_id']=i_id
                    all_data.append(i)             
                count=len(all_data)
                paginator = Paginator(all_data, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='phone_number':
                get_data = models.apply_add_main.objects.filter(phone_number__icontains=keyword_t).values('id','user_company','user_part__part_name','user_name',
                'user_identity','id_card','id_work','user_position','phone_number','short_number','status').order_by('status_time')
                all_data=[]
                for i in get_data:
                    i_id=i_id+1
                    i['i_id']=i_id
                    all_data.append(i)            
                count=len(all_data)
                paginator = Paginator(all_data, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='short_number':
                get_data = models.apply_add_main.objects.filter(short_number__icontains=keyword_t).values('id','user_company','user_part__part_name','user_name',
                'user_identity','id_card','id_work','user_position','phone_number','short_number','status').order_by('status_time')
                all_data=[]
                for i in get_data:
                    i_id=i_id+1
                    i['i_id']=i_id
                    all_data.append(i)  
                count=len(all_data)
                paginator = Paginator(all_data, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
        except:
            page = request.POST['page']
            limit = request.POST['limit']
            get_data = models.apply_add_main.objects.filter().values('id','user_company','user_part__part_name','user_name',
                'user_identity','id_card','id_work','user_position','phone_number','short_number','status').order_by('status_time')
            all_data=[]
            for i in get_data:
                i_id=i_id+1
                i['i_id']=i_id
                all_data.append(i)
            count=len(all_data)
            paginator = Paginator(all_data, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
    else:
        if user.is_staff==0:
            try:
                keyword_t = request.POST['keyword_t']
                keyword_r = request.POST['keyword_r']
                page = request.POST['page']
                limit = request.POST['limit']
                if keyword_r=='user_name':
                    get_data = models.apply_add_main.objects.filter(user_company=user.user_company,user_name__icontains=keyword_t).values('id','user_company','user_part__part_name','user_name',
                'user_identity','id_card','id_work','user_position','phone_number','short_number','status').order_by('status_time')
                    all_data=[]
                    i_id=0
                    for i in get_data:
                        i_id=i_id+1
                        i['i_id']=i_id
                        all_data.append(i)              
                    count=len(all_data)
                    paginator = Paginator(all_data, limit)        
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='phone_number':
                    get_data = models.apply_add_main.objects.filter(user_company=user.user_company,phone_number__icontains=keyword_t).values('id','user_company','user_part__part_name','user_name',
                'user_identity','id_card','id_work','user_position','phone_number','short_number','status').order_by('status_time')
                    all_data=[]
                    i_id=0
                    for i in get_data:
                        i_id=i_id+1
                        i['i_id']=i_id
                        all_data.append(i)            
                    count=len(all_data)
                    paginator = Paginator(all_data, limit)        
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='short_number':
                    get_data = models.apply_add_main.objects.filter(user_company=user.user_company,short_number__icontains=keyword_t).values('id','user_company','user_part__part_name','user_name',
                'user_identity','id_card','id_work','user_position','phone_number','short_number','status').order_by('status_time')
                    all_data=[]
                    i_id=0
                    for i in get_data:
                        i_id=i_id+1
                        i['i_id']=i_id
                        all_data.append(i)
                    for l in get_data_re:
                        i_id=i_id+1
                        l['i_id']=i_id
                        all_data.append(l)                
                    count=len(all_data)
                    paginator = Paginator(all_data, limit)        
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
            except:
                page = request.POST['page']
                limit = request.POST['limit']
                get_data = models.apply_add_main.objects.filter(user_company=user.user_company).values('id','user_company','user_part__part_name','user_name',
                'user_identity','id_card','id_work','user_position','phone_number','short_number','status').order_by('status_time')
                all_data=[]
                i_id=0
                for i in get_data:
                    i_id=i_id+1
                    i['i_id']=i_id
                    all_data.append(i)      
                count=len(all_data)
                paginator = Paginator(all_data, limit)        
                loaded = paginator.page(page)
                loaded = list(loaded)
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
        else:
            try:
                keyword_t = request.POST['keyword_t']
                keyword_r = request.POST['keyword_r']
                page = request.POST['page']
                limit = request.POST['limit']
                if keyword_r=='user_name':
                    get_data = models.apply_add_main.objects.filter(user_company=user.user_company,user_part=user.user_part,user_name__icontains=keyword_t).values('id','user_company','user_part__part_name','user_name',
                'user_identity','id_card','id_work','user_position','phone_number','short_number','status').order_by('status_time')
                    all_data=[]
                    for i in get_data:
                        i_id=i_id+1
                        i['i_id']=i_id
                        all_data.append(i)          
                    count=len(all_data)
                    paginator = Paginator(all_data, limit)        
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='phone_number':
                    get_data = models.apply_add_main.objects.filter(user_company=user.user_company,user_part=user.user_part,phone_number__icontains=keyword_t).values('id','user_company','user_part__part_name','user_name',
                'user_identity','id_card','id_work','user_position','phone_number','short_number','status').order_by('status_time')
                    all_data=[]
                    for i in get_data:
                        i_id=i_id+1
                        i['i_id']=i_id
                        all_data.append(i)           
                    count=len(all_data)
                    paginator = Paginator(all_data, limit)        
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='short_number':
                    get_data = models.apply_add_main.objects.filter(user_company=user.user_company,user_part=user.user_part,short_number__icontains=keyword_t).values('id','user_company','user_part__part_name','user_name',
                'user_identity','id_card','id_work','user_position','phone_number','short_number','status').order_by('status_time')
                    all_data=[]
                    for i in get_data:
                        i_id=i_id+1
                        i['i_id']=i_id
                        all_data.append(i)           
                    count=len(all_data)
                    paginator = Paginator(all_data, limit)        
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
            except:
                page = request.POST['page']
                limit = request.POST['limit']
                get_data = models.apply_add_main.objects.filter(user_company=user.user_company,user_part=user.user_part).values('id','user_company','user_part__part_name','user_name',
                'user_identity','id_card','id_work','user_position','phone_number','short_number','status').order_by('status_time')
                all_data=[]
                for i in get_data:
                    i_id=i_id+1
                    i['i_id']=i_id
                    all_data.append(i)
                count=len(all_data)
                paginator = Paginator(all_data, limit)        
                loaded = paginator.page(page)
                loaded = list(loaded)
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)

@login_required(login_url='/login')
def apply_alter_main(request,post_id):#变更申请
    view_list= models.number_main.objects.filter(id=post_id).values('id','user_name','id_card','id_work','user_company','user_part_id',
    'user_part__part_name','user_identity','user_position','phone_number','short_number','remark')
    view_dict = view_list[0]
    context = {
        'view_dict': view_dict,
    }
    return render(request, "manage_no/main/apply_alter_main.html",context)

@login_required(login_url='/login')
def save_alter_maindata(request):#保存变更申请
    id_pt = request.POST['id_ms']
    edit = request.POST['edit']
    user_name = request.POST['user_name']
    id_card = request.POST['id_card']
    id_work = request.POST['id_work']
    user_company = request.POST['user_company']
    user_part = request.POST['user_part']   
    user_identity = request.POST['user_identity']  
    user_position = request.POST['user_position']  
    phone_number = request.POST['phone_number']
    short_number = request.POST['short_number'] 
    if edit=='1':
        phone_number_old= models.number_main.objects.filter(id=id_pt).values('phone_number')[0]
        edit_remark='变更手机号码'+phone_number_old['phone_number']+'为'+phone_number
    elif edit=='2':
        short_number_old= models.number_main.objects.filter(id=id_pt).values('short_number')[0]
        edit_remark='变更手机短号'+short_number_old['short_number']+'为'+short_number
        models.no_pool.objects.filter(short_number=short_number).update(is_use='1')
    elif edit=='3':
        edit_remark='变更身份信息'
    models.apply_alter.objects.create(number_id=id_pt,number_type='main',user_name=user_name,id_card=id_card,id_work=id_work,user_company=user_company,user_part_id=user_part,apply_user=request.user,edit_type=edit
    ,user_identity=user_identity,status='待审批',user_position=user_position,edit_remark=edit_remark,phone_number=phone_number,short_number=short_number,status_time=datetime.now())
    return HttpResponse('{"status":"success","msg":"申请变更成功"}',content_type='application/json')

@login_required(login_url='/login')
def alter_log_main(request):#变更申请日志
    return render(request, 'manage_no/main/alter_log_main.html')

@login_required(login_url='/login')
def alter_log_maindata(request):#变更申请数据
    user = request.user
    i_id=0
    if user.is_superuser==1:
        try:
            keyword_t = request.POST['keyword_t']
            keyword_r = request.POST['keyword_r']
            page = request.POST['page']
            limit = request.POST['limit']
            if keyword_r=='user_name':
                get_num_list = models.apply_alter.objects.filter(user_name__icontains=keyword_t,number_type='main').values('id',
                'user_company','user_part__part_name','user_name','user_identity','id_card','id_work','user_position','phone_number','short_number','edit_remark','status').order_by('status_time')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='phone_number':
                get_num_list = models.apply_alter.objects.filter(phone_number__icontains=keyword_t,number_type='main').values('id',
                'user_company','user_part__part_name','user_name','user_identity','id_card','id_work','user_position','phone_number','short_number','edit_remark','status').order_by('status_time')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id                    
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='short_number':
                get_num_list = models.apply_alter.objects.filter(short_number__icontains=keyword_t,number_type='main').values('id',
                'user_company','user_part__part_name','user_name','user_identity','id_card','id_work','user_position','phone_number','short_number','edit_remark','status').order_by('status_time')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id                    
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
        except:
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.apply_alter.objects.filter(number_type='main').values('id',
                'user_company','user_part__part_name','user_name','user_identity','id_card','id_work','user_position','phone_number','short_number','edit_remark','status').order_by('status_time')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id                
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
    else:
        if user.is_staff==0:
            try:
                keyword_t = request.POST['keyword_t']
                keyword_r = request.POST['keyword_r']
                page = request.POST['page']
                limit = request.POST['limit']
                if keyword_r=='user_name':
                    get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,user_name__icontains=keyword_t,number_type='main').values('id',
                'user_company','user_part__part_name','user_name','user_identity','id_card','id_work','user_position','phone_number','short_number','edit_remark','status').order_by('status_time')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list,limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='phone_number':
                    get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,phone_number__icontains=keyword_t,number_type='main').values('id',
                'user_company','user_part__part_name','user_name','user_identity','id_card','id_work','user_position','phone_number','short_number','edit_remark','status').order_by('status_time')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list,limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='short_number':
                    get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,short_number__icontains=keyword_t,number_type='main').values('id',
                'user_company','user_part__part_name','user_name','user_identity','id_card','id_work','user_position','phone_number','short_number','edit_remark','status').order_by('status_time')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list,limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
            except:
                page = request.POST['page']
                limit = request.POST['limit']
                get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,number_type='main').values('id',
                'user_company','user_part__part_name','user_name','user_identity','id_card','id_work','user_position','phone_number','short_number','edit_remark','status').order_by('status_time')
                count=len(get_num_list)
                paginator = Paginator(get_num_list,limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
        else:
            try:
                keyword_t = request.POST['keyword_t']
                keyword_r = request.POST['keyword_r']
                page = request.POST['page']
                limit = request.POST['limit']
                if keyword_r=='user_name':
                    get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,user_part=user.user_part,user_name__icontains=keyword_t,number_type='main').values('id',
                'user_company','user_part__part_name','user_name','user_identity','id_card','id_work','user_position','phone_number','short_number','edit_remark','status').order_by('status_time')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list,limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='phone_number':
                    get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,user_part=user.user_part,phone_number__icontains=keyword_t,number_type='main').values('id',
                'user_company','user_part__part_name','user_name','user_identity','id_card','id_work','user_position','phone_number','short_number','edit_remark','status').order_by('status_time')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list,limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='short_number':
                    get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,user_part=user.user_part,short_number__icontains=keyword_t,number_type='main').values('id',
                'user_company','user_part__part_name','user_name','user_identity','id_card','id_work','user_position','phone_number','short_number','edit_remark','status').order_by('status_time')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list,limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
            except:
                page = request.POST['page']
                limit = request.POST['limit']
                get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,user_part=user.user_part,number_type='main').values('id',
                'user_company','user_part__part_name','user_name','user_identity','id_card','id_work','user_position','phone_number','short_number','edit_remark','status').order_by('status_time')
                count=len(get_num_list)
                paginator = Paginator(get_num_list,limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)

@login_required(login_url='/login')
def del_main(request,post_id):#申请删除
    view_list= models.number_main.objects.filter(id=post_id).values('id','user_name','id_card','id_work','user_company','user_part_id',
    'user_part__part_name','user_identity','user_position','phone_number','short_number')
    view_dict = view_list[0]
    context = {
        'view_dict': view_dict,
    }
    return render(request, "manage_no/main/apply_del_main.html",context)

@login_required(login_url='/login')
def save_del_maindata(request):#保存删除
    id_pt = request.POST['id_ms']
    user_name = request.POST['user_name']
    user_identity = request.POST['user_identity'] 
    user_company = request.POST['user_company']
    user_part = request.POST['user_part']
    phone_number = request.POST['phone_number'] 
    short_number = request.POST['short_number']
    reason = request.POST['reason'] 
    user_part_id = re.findall("\d+",user_part)
    models.apply_del.objects.create(number_id=id_pt,number_type='main',user_name=user_name,user_identity=user_identity,user_company=user_company
    ,user_part_id=user_part_id[0],phone_number=phone_number,short_number=short_number,reason=reason,apply_user=request.user,status='待审批',status_time=datetime.now())
    return HttpResponse('{"status":"success","msg":"申请删除成功"}',content_type='application/json')

@login_required(login_url='/login')
def del_log_main(request):#删除申请日志
    return render(request, 'manage_no/main/del_log_main.html')

@login_required(login_url='/login')
def del_log_maindata(request):#删除申请数据
    user = request.user
    i_id=0
    if user.is_superuser==1:
        page = request.POST['page']
        limit = request.POST['limit']
        get_num_list = models.apply_del.objects.filter(number_type='main').values('id','user_name','user_company','user_part_id',
        'user_part__part_name','user_identity','phone_number','short_number','reason','status').order_by('status_time')
        count=len(get_num_list)
        paginator = Paginator(get_num_list, limit)
        loaded = paginator.page(page)
        loaded = list(loaded)
        for i in loaded:
            i_id=i_id+1
            i['i_id']=i_id
        data={"code":0,"msg":"","count":count,"data":loaded}
        return JsonResponse(data,safe=False)
    else:
        if user.is_staff==0: 
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.apply_del.objects.filter(number_type='main',user_company=user.user_company).values('id','user_name','user_company','user_part_id','user_part__part_name','user_identity','phone_number','short_number','reason','status').order_by('status_time')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            data={"code":0,"msg":"","count":count,"data":loaded}
            for i in loaded:
                i_id=i_id+1
                i['i_id']=i_id

            return JsonResponse(data,safe=False)
        else:
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.apply_del.objects.filter(number_type='main',user_company=user.user_company,user_part_id=user.user_part).values('id','user_name','user_company','user_part_id','user_part__part_name','user_identity','phone_number','short_number','reason','status').order_by('status_time')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i in loaded:
                i_id=i_id+1
                i['i_id']=i_id
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)

@login_required(login_url='/login')
def main_view(request,post_id):#查看详情
    view=post_id.split('_')
    if view[0]=='add':
        view_type=1
        view_list= models.apply_add_main.objects.filter(id=view[1]).values('user_name','id_card','id_work','user_company','user_part__part_name',
        'user_identity','user_position','phone_number','short_number','status','status_time','apply_user')    
        view_dict = view_list[0]     
    elif view[0]=='alter':
        view_type=2
        view_list= models.apply_alter.objects.filter(id=view[1]).values('user_name','id_card','id_work','user_company','user_part__part_name',
        'user_identity','user_position','phone_number','short_number','status','status_time','apply_user','edit_type','edit_remark')   
        view_dict = view_list[0]
        if view_dict['edit_type']=='1':
            view_dict['edit_type']='变更主号长号'
        elif view_dict['edit_type']=='2':
            view_dict['edit_type']='变更主号短号'
        elif view_dict['edit_type']=='3':
            view_dict['edit_type']='变更基础信息'
    elif view[0]=='del':
        view_type=3
        view_list= models.apply_del.objects.filter(id=view[1]).values('user_name','user_company','user_part__part_name',
        'user_identity','phone_number','short_number','status','status_time','apply_user','reason')   
        view_dict = view_list[0]            
    context = {
        'view_dict': view_dict,
        'view_type': view_type,
    }
    return render(request, "manage_no/main/main_view.html",context)

@login_required(login_url='/login')
def save_error_main(request):#添加新增申请
    number_id = request.POST['number_id']
    error_content = request.POST['error_content']
    models.number_error.objects.create(number_id=number_id,number_type='main',error_content=error_content,apply_user=request.user,error_time=datetime.now())
    return HttpResponse('{"status":"success","msg":"报错成功"}',content_type='application/json')
#主号View End*********************************************************************************************************
#副号View *********************************************************************************************************
@login_required(login_url='/login')
def vice_no_info(request):#副号列表
    return render(request, 'manage_no/vice/vice_info.html')

@login_required(login_url='/login')
def vice_no_data(request):#副号列表数据
    user = request.user
    i_id=0
    if user.is_superuser==1:
        try:
            keyword_t = request.POST['keyword_t']
            keyword_r = request.POST['keyword_r']
            page = request.POST['page']
            limit = request.POST['limit']
            if keyword_r=='user_name':
                get_num_list = models.number_vice.objects.filter(user_name__icontains=keyword_t).values('id','user_name','id_card','id_main__user_identity',
                'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='phone_number':
                get_num_list = models.number_vice.objects.filter(phone_number__icontains=keyword_t).values('id','user_name','id_card','id_main__user_identity',
                'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='short_number':
                get_num_list = models.number_vice.objects.filter(short_number__icontains=keyword_t).values('id','user_name','id_card','id_main__user_identity',
                'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='id':
                get_num_list = models.number_vice.objects.filter(id=keyword_t).values('id','user_name','id_card','id_main__user_identity',
                'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
        except:
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.number_vice.objects.filter().values('id','user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
    else:
        if user.is_staff==0:
            try:
                keyword_t = request.POST['keyword_t']
                keyword_r = request.POST['keyword_r']
                page = request.POST['page']
                limit = request.POST['limit']
                if keyword_r=='user_name':
                    get_num_list = models.number_vice.objects.filter(user_name__icontains=keyword_t,id_main__user_company=user.user_company).values('id','user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='phone_number':
                    get_num_list = models.number_vice.objects.filter(phone_number__icontains=keyword_t,id_main__user_company=user.user_company).values('id','user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id               
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='short_number':
                    get_num_list = models.number_vice.objects.filter(short_number__icontains=keyword_t,id_main__user_company=user.user_company).values('id','user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id                
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='id':
                    get_num_list = models.number_vice.objects.filter(id=keyword_t,id_main__user_company=user.user_company).values('id','user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id                
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
            except:
                page = request.POST['page']
                limit = request.POST['limit']
                get_num_list = models.number_vice.objects.filter(id_main__user_company=user.user_company).values('id','user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
        else:
            try:
                keyword_t = request.POST['keyword_t']
                keyword_r = request.POST['keyword_r']
                page = request.POST['page']
                limit = request.POST['limit']
                if keyword_r=='user_name':
                    get_num_list = models.number_vice.objects.filter(user_name__icontains=keyword_t,id_main__user_company=user.user_company,id_main__user_part=user.user_part).values('id','user_name','id_card','id_main__user_identity',
                    'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id               
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='phone_number':
                    get_num_list = models.number_vice.objects.filter(phone_number__icontains=keyword_t,id_main__user_company=user.user_company,id_main__user_part=user.user_part).values('id','user_name','id_card','id_main__user_identity',
                    'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id             
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='short_number':
                    get_num_list = number_vice.phone_no.objects.filter(short_number__icontains=keyword_t,id_main__user_company=user.user_company,id_main__user_part=user.user_part).values('id','user_name','id_card','id_main__user_identity',
                    'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list, limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i  in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id          
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
            except:
                page = request.POST['page']
                limit = request.POST['limit']
                get_num_list = models.number_vice.objects.filter(id_main__user_company=user.user_company,id_main__user_part=user.user_part).values('id','user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('short_number')
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)

@login_required(login_url='/login')
def add_vice(request):#添加新增申请
    user = request.user
    if user.is_staff == 0:
        return render(request, "manage_no/vice/apply_add_vice.html")
    else:
        user_list = models.number_main.objects.filter(user_company=user.user_company,user_part=user.user_part).values()
        get_user_part_list = models.userext.objects.filter(id=user.id).values("user_part")
        get_user_part_dict = get_user_part_list[0]
        get_part_list = models.part.objects.filter(id=get_user_part_dict["user_part"]).values("id","part_name")
        get_part_dict = get_part_list[0]
        context = {
            'user_part_id': get_part_dict["id"],
            'user_part': get_part_dict["part_name"],
        }
        return render(request, "manage_no/vice/apply_add_vice.html",context)

@login_required(login_url='/login')
def save_add_vice(request):#添加新增申请
    try:
        id_main = request.POST['user_main']
        user_name = request.POST['user_name']
        id_card = request.POST['id_card']
        phone_number = request.POST['phone_number']
        remark = request.POST['remark']
        obj = models.number_main.objects.get(id=id_main)
        models.apply_add_vice.objects.create(id_main=obj,user_name=user_name,id_card=id_card,status='待审批',remark=remark,phone_number=phone_number,apply_user=request.user,status_time=datetime.now())
        return HttpResponse('{"status":"success","msg":"添加成功"}',content_type='application/json')
    except:
        return HttpResponse('{"status":"fail","msg":"添加失败"}',content_type='application/json')

@login_required(login_url='/login')
def add_log_vice(request):#副号新增日志
    return render(request, 'manage_no/vice/add_log_vice.html')

@login_required(login_url='/login')
def add_log_data_vice(request):#新增副号日志数据
    user = request.user
    i_id=0
    if user.is_superuser==1:
        try:
            keyword_t = request.POST['keyword_t']
            keyword_r = request.POST['keyword_r']
            page = request.POST['page']
            limit = request.POST['limit']
            if keyword_r=='user_name':
                get_data = models.apply_add_vice.objects.filter(user_name__icontains=keyword_t).values('id','user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('status_time')
                all_data=[]
                for i in get_data:
                    i_id=i_id+1
                    i['i_id']=i_id
                    all_data.append(i)          
                count=len(all_data)
                paginator = Paginator(all_data, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='phone_number':
                get_data = models.apply_add_vice.objects.filter(phone_number__icontains=keyword_t).values('id','user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('status_time')
                all_data=[]
                for i in get_data:
                    i_id=i_id+1
                    i['i_id']=i_id
                    all_data.append(i)          
                count=len(all_data)
                paginator = Paginator(all_data, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='short_number':
                get_data = models.apply_add_vice.objects.filter(short_number__icontains=keyword_t).values('id','user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name').order_by('status_time')
                all_data=[]
                for i in get_data:
                    i_id=i_id+1
                    i['i_id']=i_id
                    all_data.append(i)           
                count=len(all_data)
                paginator = Paginator(all_data, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
        except:
            page = request.POST['page']
            limit = request.POST['limit']
            get_data = models.apply_add_vice.objects.filter().values('id','user_name','id_card','id_main__user_identity','phone_number','short_number',
            'id_main__user_company','id_main__user_part__part_name','id_main__user_name','status').order_by('status_time')
            all_data=[]
            for i in get_data:
                i_id=i_id+1
                i['i_id']=i_id
                all_data.append(i)        
            count=len(all_data)
            paginator = Paginator(all_data, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
    else:
        if user.is_staff==0:
            try:
                keyword_t = request.POST['keyword_t']
                keyword_r = request.POST['keyword_r']
                page = request.POST['page']
                limit = request.POST['limit']
                if keyword_r=='user_name':
                    get_data = models.apply_add_vice.objects.filter(user_company=user.user_company,user_name__icontains=keyword_t).values('user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id','id_main__user_name').order_by('status_time')
                    all_data=[]
                    i_id=0
                    for i in get_data:
                        i_id=i_id+1
                        i['i_id']=i_id
                        all_data.append(i)            
                    count=len(all_data)
                    paginator = Paginator(all_data, limit)        
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='phone_number':
                    get_data = models.apply_add_vice.objects.filter(user_company=user.user_company,phone_number__icontains=keyword_t).values('user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id','id_main__user_name').order_by('status_time')
                    all_data=[]
                    i_id=0
                    for i in get_data:
                        i_id=i_id+1
                        i['i_id']=i_id
                        all_data.append(i)            
                    count=len(all_data)
                    paginator = Paginator(all_data, limit)        
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='short_number':
                    get_data = models.apply_add_vice.objects.filter(user_company=user.user_company,short_number__icontains=keyword_t).values('user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id','id_main__user_name').order_by('status_time')
                    all_data=[]
                    i_id=0
                    for i in get_data:
                        i_id=i_id+1
                        i['i_id']=i_id
                        all_data.append(i)         
                    count=len(all_data)
                    paginator = Paginator(all_data, limit)        
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
            except:
                page = request.POST['page']
                limit = request.POST['limit']
                get_data = models.apply_add_vice.objects.filter(id_main__user_company=user.user_company).values('user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id','id_main__user_name').order_by('status_time')
                all_data=[]
                i_id=0
                for i in get_data:
                    i_id=i_id+1
                    i['i_id']=i_id
                    all_data.append(i)             
                count=len(all_data)
                paginator = Paginator(all_data, limit)        
                loaded = paginator.page(page)
                loaded = list(loaded)
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
        else:
            try:
                keyword_t = request.POST['keyword_t']
                keyword_r = request.POST['keyword_r']
                page = request.POST['page']
                limit = request.POST['limit']
                if keyword_r=='user_name':
                    get_data = models.apply_add_vice.objects.filter(user_company=user.user_company,id_main__user_part=user.user_part,user_name__icontains=keyword_t).values('user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id','id_main__user_name').order_by('status_time')
                    all_data=[]
                    for i in get_data:
                        i_id=i_id+1
                        i['i_id']=i_id
                        all_data.append(i)             
                    count=len(all_data)
                    paginator = Paginator(all_data, limit)        
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='phone_number':
                    get_data = models.apply_add_vice.objects.filter(user_company=user.user_company,id_main__user_part=user.user_part,phone_number__icontains=keyword_t).values('user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id','id_main__user_name').order_by('status_time')
                    all_data=[]
                    for i in get_data:
                        i_id=i_id+1
                        i['i_id']=i_id
                        all_data.append(i)             
                    count=len(all_data)
                    paginator = Paginator(all_data, limit)        
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='short_number':
                    get_data = models.apply_add_vice.objects.filter(user_company=user.user_company,id_main__user_part=user.user_part,short_number__icontains=keyword_t).values('user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id','id_main__user_name').order_by('status_time')
                    all_data=[]
                    for i in get_data:
                        i_id=i_id+1
                        i['i_id']=i_id
                        all_data.append(i)              
                    count=len(all_data)
                    paginator = Paginator(all_data, limit)        
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
            except:
                page = request.POST['page']
                limit = request.POST['limit']
                get_data = models.apply_add_vice.objects.filter(id_main__user_company=user.user_company,id_main__user_part=user.user_part).values('user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id','id_main__user_name').order_by('status_time')
                all_data=[]
                for i in get_data:
                    i_id=i_id+1
                    i['i_id']=i_id
                    all_data.append(i)              
                count=len(all_data)
                paginator = Paginator(all_data, limit)        
                loaded = paginator.page(page)
                loaded = list(loaded)
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)

@login_required(login_url='/login')
def apply_alter_vice(request,post_id):#变更申请
    view_list= models.number_vice.objects.filter(id=post_id).values('id','user_name','id_card','id_main__user_identity',
    'phone_number','short_number','id_main__user_company','id_main__user_part','id_main__user_part__part_name','id_main__user_name','remark')
    view_dict = view_list[0]
    context = {
        'view_dict': view_dict,
    }
    return render(request, "manage_no/vice/apply_alter_vice.html",context)

@login_required(login_url='/login')
def save_alter_vicedata(request):#保存变更申请
    id_pt = request.POST['id_ms']
    edit = request.POST['edit']
    user_name = request.POST['user_name']
    id_card = request.POST['id_card']
    phone_number = request.POST['phone_number']
    short_number = request.POST['short_number'] 
    user_identity = request.POST['user_identity']
    user_company = request.POST['user_company']
    user_part = request.POST['user_part'] 
    id_main = request.POST['user_main'] 
    if edit=='1':
        phone_number_old= models.number_vice.objects.filter(id=id_pt).values('phone_number')[0]
        edit_remark='变更手机号码'+phone_number_old['phone_number']+'为'+phone_number
    elif edit=='2':
        short_number_old= models.number_vice.objects.filter(id=id_pt).values('short_number')[0]
        edit_remark='变更手机短号'+short_number_old['short_number']+'为'+short_number
        models.no_pool.objects.filter(short_number=short_number).update(is_use='1')
    elif edit=='3':
        edit_remark='变更身份信息'
    elif edit=='4':
        id_main_new= models.number_main.objects.filter(id=id_main).values('user_name','user_part__part_name')[0]
        str_new = user_company+id_main_new['user_part__part_name']+id_main_new['user_name']+'('+str(id_main)+')'
        id_main_old= models.number_vice.objects.filter(id=id_pt).values('id_main','id_main__user_name','id_main__user_company','id_main__user_part','id_main__user_part__part_name')[0]
        if user_identity=='员工类别3':
            str_old = id_main_old['id_main__user_name']+'('+str(id_main_old['id_main'])+')'
        else:
            str_old = id_main_old['id_main__user_company']+id_main_old['id_main__user_part__part_name']+id_main_old['id_main__user_name']+'('+str(id_main_old['id_main'])+')'
        edit_remark='变更主号人员'+str_old+'为'+str_new
    if user_identity=='员工类别3':
         models.apply_alter.objects.create(number_id=id_pt,number_type='vice',user_name=user_name,id_card=id_card,apply_user=request.user,
            status='待审批',edit_type=edit,user_identity=user_identity,user_company=user_company,edit_remark=edit_remark,phone_number=phone_number,short_number=short_number,status_time=datetime.now())
    else:
        part_id = models.part.objects.get(id=user_part)
        models.apply_alter.objects.create(number_id=id_pt,number_type='vice',user_name=user_name,id_card=id_card,apply_user=request.user,
            status='待审批',edit_type=edit,user_identity=user_identity,user_company=user_company,user_part=part_id,edit_remark=edit_remark,phone_number=phone_number,short_number=short_number,status_time=datetime.now())
    return HttpResponse('{"status":"success","msg":"申请变更成功"}',content_type='application/json')

@login_required(login_url='/login')
def alter_log_vice(request):#变更申请日志
    return render(request, 'manage_no/vice/alter_log_vice.html')

@login_required(login_url='/login')
def alter_log_vicedata(request):#变更申请数据
    user = request.user
    i_id=0
    if user.is_superuser==1:
        try:
            keyword_t = request.POST['keyword_t']
            keyword_r = request.POST['keyword_r']
            page = request.POST['page']
            limit = request.POST['limit']
            if keyword_r=='user_name':
                get_num_list = models.apply_alter.objects.filter(user_name__icontains=keyword_t,number_type='vice').values()
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='phone_number':
                get_num_list = models.apply_alter.objects.filter(phone_number__icontains=keyword_t,number_type='vice').values()
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id                    
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
            elif keyword_r=='short_number':
                get_num_list = models.apply_alter.objects.filter(short_number__icontains=keyword_t,number_type='vice').values()
                count=len(get_num_list)
                paginator = Paginator(get_num_list, limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i  in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id                    
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
        except:
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.apply_alter.objects.filter(number_type='vice').values()
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i  in loaded:
                i_id=i_id+1
                i['i_id']=i_id                
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)
    else:
        if user.is_staff==0:
            try:
                keyword_t = request.POST['keyword_t']
                keyword_r = request.POST['keyword_r']
                page = request.POST['page']
                limit = request.POST['limit']
                if keyword_r=='user_name':
                    get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,user_name__icontains=keyword_t,number_type='vice').values()
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list,limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='phone_number':
                    get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,phone_number__icontains=keyword_t,number_type='vice').values()
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list,limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='short_number':
                    get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,short_number__icontains=keyword_t,number_type='vice').values()
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list,limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
            except:
                page = request.POST['page']
                limit = request.POST['limit']
                get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,number_type='vice').values()
                count=len(get_num_list)
                paginator = Paginator(get_num_list,limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)
        else:
            try:
                keyword_t = request.POST['keyword_t']
                keyword_r = request.POST['keyword_r']
                page = request.POST['page']
                limit = request.POST['limit']
                if keyword_r=='user_name':
                    get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,user_part=user.user_part,user_name__icontains=keyword_t,number_type='vice').values()
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list,limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='phone_number':
                    get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,user_part=user.user_part,phone_number__icontains=keyword_t,number_type='vice').values()
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list,limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
                elif keyword_r=='short_number':
                    get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,user_part=user.user_part,short_number__icontains=keyword_t,number_type='vice').values()
                    count=len(get_num_list)
                    paginator = Paginator(get_num_list,limit)
                    loaded = paginator.page(page)
                    loaded = list(loaded)
                    for i in loaded:
                        i_id=i_id+1
                        i['i_id']=i_id
                    data={"code":0,"msg":"","count":count,"data":loaded}
                    return JsonResponse(data,safe=False)
            except:
                page = request.POST['page']
                limit = request.POST['limit']
                get_num_list = models.apply_alter.objects.filter(user_company=user.user_company,user_part=user.user_part,number_type='vice').values()
                count=len(get_num_list)
                paginator = Paginator(get_num_list,limit)
                loaded = paginator.page(page)
                loaded = list(loaded)
                for i in loaded:
                    i_id=i_id+1
                    i['i_id']=i_id
                data={"code":0,"msg":"","count":count,"data":loaded}
                return JsonResponse(data,safe=False)

@login_required(login_url='/login')
def del_vice(request,post_id):#申请删除
    view_list= models.number_vice.objects.filter(id=post_id).values('id','user_name','id_card','id_main__user_identity',
    'phone_number','short_number','id_main__user_company','id_main__user_part','id_main__user_part__part_name','id_main__user_name','remark')
    view_dict = view_list[0]
    context = {
        'view_dict': view_dict,
    }
    return render(request, "manage_no/vice/apply_del_vice.html",context)

@login_required(login_url='/login')
def save_del_vicedata(request):#保存删除
    id_pt = request.POST['id_ms']
    user_name = request.POST['user_name']
    user_company = request.POST['user_company'] 
    user_part = request.POST['user_part']
    user_identity = request.POST['user_identity']
    phone_number = request.POST['phone_number'] 
    short_number = request.POST['short_number']
    reason = request.POST['reason'] 
    user_part_id = re.findall("\d+",user_part)
    if user_identity=='员工类别3':
        models.apply_del.objects.create(number_id=id_pt,number_type='vice',user_identity=user_identity,user_company=user_company,user_name=user_name,phone_number=phone_number,short_number=short_number,reason=reason,apply_user=request.user,status='待审批',status_time=datetime.now())
    else:
        part_id = models.part.objects.get(id=user_part_id[0])
        models.apply_del.objects.create(number_id=id_pt,number_type='vice',user_identity=user_identity,user_company=user_company,user_part=part_id,user_name=user_name,phone_number=phone_number,short_number=short_number,reason=reason,apply_user=request.user,status='待审批',status_time=datetime.now())
    return HttpResponse('{"status":"success","msg":"申请删除成功"}',content_type='application/json')

@login_required(login_url='/login')
def del_log_vice(request):#删除申请日志
    return render(request, 'manage_no/vice/del_log_vice.html')

@login_required(login_url='/login')
def del_log_vicedata(request):#删除申请数据
    user = request.user
    i_id=0
    if user.is_superuser==1:
        page = request.POST['page']
        limit = request.POST['limit']
        get_num_list = models.apply_del.objects.filter(number_type='vice').values('id','user_name','phone_number','short_number','reason','status').order_by('status_time')
        count=len(get_num_list)
        paginator = Paginator(get_num_list, limit)
        loaded = paginator.page(page)
        loaded = list(loaded)
        for i in loaded:
            i_id=i_id+1
            i['i_id']=i_id
        data={"code":0,"msg":"","count":count,"data":loaded}
        return JsonResponse(data,safe=False)
    else:
        if user.is_staff==0: 
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.apply_del.objects.filter(number_type='vice',user_company=user.user_company).values('id','user_name','phone_number','short_number','reason','status').order_by('status_time')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            data={"code":0,"msg":"","count":count,"data":loaded}
            for i in loaded:
                i_id=i_id+1
                i['i_id']=i_id

            return JsonResponse(data,safe=False)
        else:
            page = request.POST['page']
            limit = request.POST['limit']
            get_num_list = models.apply_del.objects.filter(number_type='vice',user_company=user.user_company,user_part_id=user.user_part).values('id','user_name','phone_number','short_number','reason','status').order_by('status_time')
            count=len(get_num_list)
            paginator = Paginator(get_num_list, limit)
            loaded = paginator.page(page)
            loaded = list(loaded)
            for i in loaded:
                i_id=i_id+1
                i['i_id']=i_id
            data={"code":0,"msg":"","count":count,"data":loaded}
            return JsonResponse(data,safe=False)

@login_required(login_url='/login')
def vice_view(request,post_id):#查看详情
    view=post_id.split('_')
    if view[0]=='add':
        view_type=1
        view_list= models.apply_add_vice.objects.filter(id=view[1]).values('user_name','id_card','id_main__user_company','id_main__user_part__part_name',
        'id_main__user_identity','id_main__user_name','phone_number','short_number','status','status_time','apply_user')    
        view_dict = view_list[0]     
    elif view[0]=='alter':
        view_type=2
        view_list= models.apply_alter.objects.filter(id=view[1]).values()   
        view_dict = view_list[0]
        if view_dict['edit_type']=='1':
            view_dict['edit_type']='变更主号长号'
        elif view_dict['edit_type']=='2':
            view_dict['edit_type']='变更主号短号'
        elif view_dict['edit_type']=='3':
            view_dict['edit_type']='变更基础信息'
        elif view_dict['edit_type']=='4':
            view_dict['edit_type']='变更主号人员'
    elif view[0]=='del':
        view_type=3
        view_list= models.apply_del.objects.filter(id=view[1]).values()   
        view_dict = view_list[0]            
    context = {
        'view_dict': view_dict,
        'view_type': view_type,
    }
    return render(request, "manage_no/vice/vice_view.html",context)

@login_required(login_url='/login')
def save_error_vice(request):#添加新增申请
    number_id = request.POST['number_id']
    error_content = request.POST['error_content']
    models.number_error.objects.create(number_id=number_id,number_type='vice',error_content=error_content,apply_user=request.user,error_time=datetime.now())
    return HttpResponse('{"status":"success","msg":"报错成功"}',content_type='application/json')
#副号View End*********************************************************************************************************

#审批View *********************************************************************************************************
@staff_only
@login_required(login_url='/login')
def apply_approval(request):#审批
    return render(request, 'manage_no/approval/apply_approval.html')

@staff_only
@login_required(login_url='/login')
def apply_approval_data(request):#审批
    page = request.POST['page']
    limit = request.POST['limit']
    user = request.user
    if user.is_superuser==1:
        id_rm = 0
        get_num_list = []
        get_apply_addmain_list = models.apply_add_main.objects.filter(status='待审批').values('id','user_name','user_identity','user_company','user_part__part_name','phone_number','status_time')
        for a in get_apply_addmain_list:
            id_rm = id_rm + 1
            a['id_rm']=id_rm
            a['short_number']='待分配'
            a['edit_remark']='主号新增'
            a['type'] = 'addmain'
            if a['user_identity']=='员工类别3':
                a['user_part']='员工类别3'
            else:
                a['user_part']=a['user_part__part_name']
            get_num_list.append(a)
        get_apply_addvice_list = models.apply_add_vice.objects.filter(status='待审批').values('id','user_name','id_main__user_identity','phone_number','id_main__user_company','id_main__user_part__part_name','status_time')
        for b in get_apply_addvice_list:
            id_rm = id_rm + 1
            b['id_rm']=id_rm
            b['edit_remark']='副号新增'
            b['short_number']='待分配'
            b['type'] = 'addvice'
            b['user_company'] = b['id_main__user_company']
            if b['id_main__user_identity']=='员工类别3':
                b['user_part']='员工类别3'
            else:
                b['user_part']=b['id_main__user_part__part_name']
            get_num_list.append(b)
        get_apply_alter_list = models.apply_alter.objects.filter(status='待审批').values('id','edit_remark','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
        for c in get_apply_alter_list:
            id_rm = id_rm + 1
            c['id_rm']=id_rm
            c['type'] = 'alter'
            if c['user_identity']=='员工类别3':
                c['user_part']='员工类别3'
            else:
                c['user_part']=c['user_part__part_name']
            get_num_list.append(c)
        get_apply_del_list = models.apply_del.objects.filter(status='待审批').values('id','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
        for d in get_apply_del_list:
            id_rm = id_rm + 1
            d['id_rm']=id_rm
            d['edit_remark']='删除'
            d['type'] = 'del'
            if d['user_identity']=='员工类别3':
                d['user_part']='员工类别3'
            else:
                d['user_part']=d['user_part__part_name']
            get_num_list.append(d)
    else:
        id_rm = 0
        get_num_list = []
        get_apply_addmain_list = models.apply_add_main.objects.filter(status='待审批',user_company=user.user_company).values('id','user_name','user_identity','user_company','user_part__part_name','phone_number','status_time')
        for a in get_apply_addmain_list:
            id_rm = id_rm + 1
            a['id_rm']=id_rm
            a['short_number']='待分配'
            a['edit_remark']='主号新增'
            a['type'] = 'addmain'
            if a['user_identity']=='员工类别3':
                a['user_part']='员工类别3'
            else:
                a['user_part']=a['user_part__part_name']
            get_num_list.append(a)
        get_apply_addvice_list = models.apply_add_vice.objects.filter(status='待审批',id_main__user_company=user.user_company).values('id','user_name','id_main__user_identity','phone_number','id_main__user_company','id_main__user_part__part_name','status_time')
        for b in get_apply_addvice_list:
            id_rm = id_rm + 1
            b['id_rm']=id_rm
            b['edit_remark']='副号新增'
            b['short_number']='待分配'
            b['type'] = 'addvice'
            b['user_company'] = b['id_main__user_company']
            if b['id_main__user_identity']=='员工类别3':
                b['user_part']='员工类别3'
            else:
                b['user_part']=b['id_main__user_part__part_name']
            get_num_list.append(b)
        get_apply_alter_list = models.apply_alter.objects.filter(status='待审批',user_company=user.user_company).values('id','edit_remark','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
        for c in get_apply_alter_list:
            id_rm = id_rm + 1
            c['id_rm']=id_rm
            c['type'] = 'alter'
            if c['user_identity']=='员工类别3':
                c['user_part']='员工类别3'
            else:
                c['user_part']=c['user_part__part_name']
            get_num_list.append(c)
        get_apply_del_list = models.apply_del.objects.filter(status='待审批',user_company=user.user_company).values('id','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
        for d in get_apply_del_list:
            id_rm = id_rm + 1
            d['id_rm']=id_rm
            d['edit_remark']='删除'
            d['type'] = 'del'
            if d['user_identity']=='员工类别3':
                d['user_part']='员工类别3'
            else:
                d['user_part']=d['user_part__part_name']
            get_num_list.append(d)
    count=len(get_num_list)
    paginator = Paginator(get_num_list, limit)
    loaded = paginator.page(page)
    loaded = list(loaded)
    data={"code":0,"msg":"","count":count,"data":loaded}
    return JsonResponse(data,safe=False)

@staff_only
@login_required(login_url='/login')
def apply_is_approval(request,post_id):#审批详情
    apply_it=post_id.split('_')
    user = request.user
    approval_type=apply_it[1]
    if user.is_superuser==1: 
        if approval_type=='addmain':
            view_list= models.apply_add_main.objects.filter(id=apply_it[0]).values('id','user_name','id_card','id_work','user_company',
            'user_part__part_name','user_identity','user_position','phone_number','remark','apply_user','status_time')
            short_num = models.no_pool.objects.filter(is_use='0').values('short_number').order_by('short_number')
            context = {
                'view_dict': view_list[0],
                'short_num': short_num,
                'view_type': 1,
                'supper': 1,
                'id_xh': post_id,
            }
            return render(request, 'manage_no/approval/apply_add_isapproval.html',context)
        elif approval_type=='addvice':
            view_list = models.apply_add_vice.objects.filter(id=apply_it[0]).values('id','user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name','remark','apply_user','status_time')
            short_num = models.no_pool.objects.filter(is_use='0',is_re='1').values('short_number').order_by('short_number')
            context = {
                'view_dict': view_list[0],
                'short_num': short_num,
                'view_type':2,
                'supper': 1,
                'id_xh': post_id,
            }
            return render(request, 'manage_no/approval/apply_add_isapproval.html',context)
        elif approval_type=='alter':
            view_list = models.apply_alter.objects.filter(id=apply_it[0]).values('number_id','number_type','user_name','id_card','id_work','user_company'
            ,'user_part__part_name','user_identity','user_position','phone_number','short_number','edit_type','edit_remark','apply_user','status_time')
            context = {
                'view_dict': view_list[0],
                'view_type':2,
                'supper': 1,
                'id_xh': apply_it[0],
            }
            return render(request, 'manage_no/approval/apply_alter_isapproval.html',context)
        elif approval_type=='del':
            view_list = models.apply_del.objects.filter(id=apply_it[0]).values('number_id','number_type','user_name','user_company'
            ,'user_part__part_name','user_identity','reason','phone_number','short_number','apply_user','status_time')
            context = {
                'view_dict': view_list[0],
                'supper': 1,
                'id_xh': apply_it[0],
            }
            return render(request, 'manage_no/approval/apply_del_isapproval.html',context)
    else:
        if approval_type=='addmain':
            view_list= models.apply_add_main.objects.filter(id=apply_it[0]).values('id','user_name','id_card','id_work','user_company',
            'user_part__part_name','user_identity','user_position','phone_number','remark','apply_user','status_time')
            short_num = models.no_pool.objects.filter(belong=user.user_company,is_use='0').values('short_number').order_by('short_number')
            context = {
                'view_dict': view_list[0],
                'short_num': short_num,
                'view_type': 1,
                'id_xh': post_id,
            }
            return render(request, 'manage_no/approval/apply_add_isapproval.html',context)
        elif approval_type=='addvice':
            view_list = models.apply_add_vice.objects.filter(id=apply_it[0]).values('id','user_name','id_card','id_main__user_identity',
            'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name','remark','apply_user','status_time')
            short_num = models.no_pool.objects.filter(belong=user.user_company,is_use='0',is_re='1').values('short_number').order_by('short_number')
            context = {
                'view_dict': view_list[0],
                'short_num': short_num,
                'view_type':2,
                'id_xh': post_id,
            }
            return render(request, 'manage_no/approval/apply_add_isapproval.html',context)
        elif approval_type=='alter':
            view_list = models.apply_alter.objects.filter(id=apply_it[0]).values('number_id','number_type','user_name','id_card','id_work','user_company'
            ,'user_part__part_name','user_identity','user_position','phone_number','short_number','edit_type','edit_remark','apply_user','status_time')
            context = {
                'view_dict': view_list[0],
                'view_type':2,
                'id_xh': apply_it[0],
            }
            return render(request, 'manage_no/approval/apply_alter_isapproval.html',context)
        elif approval_type=='del':
            view_list = models.apply_del.objects.filter(id=apply_it[0]).values('number_id','number_type','user_name','user_company'
            ,'user_part__part_name','user_identity','reason','phone_number','short_number','apply_user','status_time')
            context = {
                'view_dict': view_list[0],
                'id_xh': apply_it[0],
            }
            return render(request, 'manage_no/approval/apply_del_isapproval.html',context)

@staff_only
@login_required(login_url='/login')
def approval_add_yes(request):#保存审批
    id_pt = request.POST['id_xh']
    apply_it=id_pt.split('_')
    approval_type=apply_it[1]
    permission_time =  datetime.now()
    if request.META.get('HTTP_X_FORWARDED_FOR'):
        ip_address = request.META.get("HTTP_X_FORWARDED_FOR")
    else:
        ip_address = request.META.get("REMOTE_ADDR")
    user = request.user
    if user.is_superuser==1:
        is_bl = request.POST['is_bl']
    else:
        is_bl='0'  
    short_number = request.POST['short_number']
    if is_bl=='0':
        if approval_type =='addmain':
            models.apply_add_main.objects.filter(id=apply_it[0]).update(short_number=short_number,status='待办理',status_time=datetime.now(),ip_address=ip_address)
        elif approval_type =='addvice':
            models.apply_add_vice.objects.filter(id=apply_it[0]).update(short_number=short_number,status='待办理',status_time=datetime.now(),ip_address=ip_address)
    else:
        if approval_type =='addmain':
            models.apply_add_main.objects.filter(id=apply_it[0]).update(short_number=short_number,status='待办理_补单',status_time=datetime.now(),ip_address=ip_address)
        elif approval_type =='addvice':
            models.apply_add_vice.objects.filter(id=apply_it[0]).update(short_number=short_number,status='待办理_补单',status_time=datetime.now(),ip_address=ip_address)
    models.no_pool.objects.filter(short_number=short_number).update(is_use='1')
    return HttpResponse('{"status":"success","msg":"审批成功"}',content_type='application/json')
@staff_only
@login_required(login_url='/login')
def approval_add_no(request):#保存审批
    id_pt = request.POST['id_xh']
    apply_it=id_pt.split('_')
    approval_type=apply_it[1]
    if request.META.get('HTTP_X_FORWARDED_FOR'):
        ip_address = request.META.get("HTTP_X_FORWARDED_FOR")
    else:
        ip_address = request.META.get("REMOTE_ADDR")
    if approval_type =='addmain':
        models.apply_add_main.objects.filter(id=apply_it[0]).update(fail_reason='审批不通过',status='失败',status_time=datetime.now(),ip_address=ip_address)
    elif approval_type =='addvice':
        models.apply_add_vice.objects.filter(id=apply_it[0]).update(fail_reason='审批不通过',status='失败',status_time=datetime.now(),ip_address=ip_address)
    return HttpResponse('{"status":"success","msg":"不予审批成功"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def approval_alter_yes(request):#保存审批
    id_pt = request.POST['id_xh']
    permission_time =  datetime.now()
    get_type_list = models.apply_alter.objects.filter(id=id_pt).values()
    get_type_dict = get_type_list[0]
    if request.META.get('HTTP_X_FORWARDED_FOR'):
        ip_address = request.META.get("HTTP_X_FORWARDED_FOR")
    else:
        ip_address = request.META.get("REMOTE_ADDR")
    user = request.user
    if user.is_superuser==1:
        is_bl = request.POST['is_bl']
    else:
        is_bl='0'  
    if is_bl=='0':
        if get_type_dict['number_type']=='main':
            if get_type_dict['edit_type']=='3':
                models.number_main.objects.filter(id=get_type_dict['number_id']).update(id_card=get_type_dict['id_card']
                ,id_work=get_type_dict['id_work'],user_company=get_type_dict['user_company'],user_part_id=get_type_dict['user_part_id'],user_identity=get_type_dict['user_identity'],user_position=get_type_dict['user_position'])
                models.apply_alter.objects.filter(id=id_pt).update(status='已完成',status_time=datetime.now(),ip_address=ip_address)
            else:
                models.apply_alter.objects.filter(id=id_pt).update(status='待办理',status_time=datetime.now(),ip_address=ip_address)
        else:
            if get_type_dict['edit_type']=='3':
                models.number_vice.objects.filter(id=get_type_dict['number_id']).update(user_name=get_type_dict['user_name'],id_card=get_type_dict['id_card'])
                models.apply_alter.objects.filter(id=id_pt).update(status='已完成',status_time=datetime.now(),ip_address=ip_address)
            elif get_type_dict['edit_type']=='4':
                get_id_list = get_type_dict['edit_remark'].split('(')
                get_id = get_id_list[2]
                models.number_vice.objects.filter(id=get_type_dict['number_id']).update(id_main=get_id[0:-1])
                models.apply_alter.objects.filter(id=id_pt).update(status='已完成',status_time=datetime.now(),ip_address=ip_address)
            else:
                models.apply_alter.objects.filter(id=id_pt).update(status='待办理',status_time=datetime.now(),ip_address=ip_address)
    else:
        if get_type_dict['number_type']=='main':
            if get_type_dict['edit_type']=='3':
                models.number_main.objects.filter(id=get_type_dict['number_id']).update(id_card=get_type_dict['id_card']
                ,id_work=get_type_dict['id_work'],user_company=get_type_dict['user_company'],user_part_id=get_type_dict['user_part_id'],user_identity=get_type_dict['user_identity'],user_position=get_type_dict['user_position'])
                models.apply_alter.objects.filter(id=id_pt).update(status='已完成',status_time=datetime.now(),ip_address=ip_address)
            else:
                models.apply_alter.objects.filter(id=id_pt).update(status='待办理_补单',status_time=datetime.now(),ip_address=ip_address)
        else:
            if get_type_dict['edit_type']=='3':
                models.number_vice.objects.filter(id=get_type_dict['number_id']).update(user_name=get_type_dict['user_name'],id_card=get_type_dict['id_card'],remark=get_type_dict['remark'])
                models.apply_alter.objects.filter(id=id_pt).update(status='已完成',status_time=datetime.now(),ip_address=ip_address)
            elif get_type_dict['edit_type']=='4':
                get_id_list = get_type_dict['edit_remark'].split('(')
                get_id = get_id_list[2]
                models.number_vice.objects.filter(id=get_type_dict['number_id']).update(id_main=get_id[0:-1])
                models.apply_alter.objects.filter(id=id_pt).update(status='已完成',status_time=datetime.now(),ip_address=ip_address)
            else:
                models.apply_alter.objects.filter(id=id_pt).update(status='待办理_补单',status_time=datetime.now(),ip_address=ip_address)
    return HttpResponse('{"status":"success","msg":"审批成功"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def approval_alter_no(request):#保存审批
    id_pt = request.POST['id_xh']
    if request.META.get('HTTP_X_FORWARDED_FOR'):
        ip_address = request.META.get("HTTP_X_FORWARDED_FOR")
    else:
        ip_address = request.META.get("REMOTE_ADDR")
    models.apply_alter.objects.filter(id=id_pt).update(status='失败',fail_reason='审批未通过',status_time=datetime.now(),ip_address=ip_address)
    return HttpResponse('{"status":"success","msg":"不予审批成功"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def approval_del_yes(request):#保存审批
    id_pt = request.POST['id_xh']
    if request.META.get('HTTP_X_FORWARDED_FOR'):
        ip_address = request.META.get("HTTP_X_FORWARDED_FOR")
    else:
        ip_address = request.META.get("REMOTE_ADDR")
    user = request.user
    if user.is_superuser==1:
        is_bl = request.POST['is_bl']
    else:
        is_bl='0'  
    if is_bl=='0':
        models.apply_del.objects.filter(id=id_pt).update(status='待办理',status_time=datetime.now(),ip_address=ip_address)
    else:
        models.apply_del.objects.filter(id=id_pt).update(status='待办理_补单',status_time=datetime.now(),ip_address=ip_address)
    return HttpResponse('{"status":"success","msg":"审批成功"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def approval_del_no(request):#保存审批
    id_pt = request.POST['id_xh']
    if request.META.get('HTTP_X_FORWARDED_FOR'):
        ip_address = request.META.get("HTTP_X_FORWARDED_FOR")
    else:
        ip_address = request.META.get("REMOTE_ADDR")
    permission_time =  datetime.now()
    models.apply_del.objects.filter(id=id_pt).update(status='失败',fail_reason='审批未通过',status_time=datetime.now(),ip_address=ip_address)
    return HttpResponse('{"status":"success","msg":"不予审批成功"}',content_type='application/json')

@staff_only
@login_required(login_url='/login')
def apply_fail(request):#失败原因
    return render(request, 'manage_no/apply_fail.html')

@staff_only
@login_required(login_url='/login')
def apply_fail_data(request):#失败原因
    page = request.POST['page']
    limit = request.POST['limit']
    user = request.user
    if user.is_superuser==1:
        id_rm = 0
        get_num_list = []
        get_apply_addmain_list = models.apply_add_main.objects.filter(status='失败').values('id','user_name','user_identity','user_company','user_part__part_name','short_number','phone_number','fail_reason','status_time')
        for a in get_apply_addmain_list:
            id_rm = id_rm + 1
            a['id_rm']=id_rm
            a['edit_remark']='主号新增'
            a['type'] = 'addmain'
            if a['user_identity']=='员工类别3':
                a['user_part']='员工类别3'
            else:
                a['user_part']=a['user_part__part_name']
            get_num_list.append(a)
        get_apply_addvice_list = models.apply_add_vice.objects.filter(status='失败').values('id','user_name','id_main__user_identity','phone_number','id_main__user_company','short_number','id_main__user_part__part_name','fail_reason','status_time')
        for b in get_apply_addvice_list:
            id_rm = id_rm + 1
            b['id_rm']=id_rm
            b['edit_remark']='副号新增'
            b['type'] = 'addvice'
            b['user_company'] = b['id_main__user_company']
            if b['id_main__user_identity']=='员工类别3':
                b['user_part']='员工类别3'
            else:
                b['user_part']=b['id_main__user_part__part_name']
            get_num_list.append(b)
        get_apply_alter_list = models.apply_alter.objects.filter(status='失败').values('id','edit_remark','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','fail_reason','status_time')
        for c in get_apply_alter_list:
            id_rm = id_rm + 1
            c['id_rm']=id_rm
            c['type'] = 'alter'
            if c['user_identity']=='员工类别3':
                c['user_part']='员工类别3'
            else:
                c['user_part']=c['user_part__part_name']
            get_num_list.append(c)
        get_apply_del_list = models.apply_del.objects.filter(status='失败').values('id','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','fail_reason','status_time')
        for d in get_apply_del_list:
            id_rm = id_rm + 1
            d['id_rm']=id_rm
            d['edit_remark']='删除'
            d['type'] = 'del'
            if d['user_identity']=='员工类别3':
                d['user_part']='员工类别3'
            else:
                d['user_part']=d['user_part__part_name']
            get_num_list.append(d)
    else:
        id_rm = 0
        get_num_list = []
        get_apply_addmain_list = models.apply_add_main.objects.filter(status='失败').values('id','user_name','short_number','user_identity','user_company','user_part__part_name','phone_number','fail_reason','status_time')
        for a in get_apply_addmain_list:
            id_rm = id_rm + 1
            a['id_rm']=id_rm
            a['edit_remark']='主号新增'
            a['type'] = 'addmain'
            if a['user_identity']=='员工类别3':
                a['user_part']='员工类别3'
            else:
                a['user_part']=a['user_part__part_name']
            get_num_list.append(a)
        get_apply_addvice_list = models.apply_add_vice.objects.filter(status='失败').values('id','user_name','short_number','id_main__user_identity','phone_number','id_main__user_company','id_main__user_part__part_name','fail_reason','status_time')
        for b in get_apply_addvice_list:
            id_rm = id_rm + 1
            b['id_rm']=id_rm
            b['edit_remark']='副号新增'
            b['type'] = 'addvice'
            b['user_company'] = b['id_main__user_company']
            if b['id_main__user_identity']=='员工类别3':
                b['user_part']='员工类别3'
            else:
                b['user_part']=b['id_main__user_part__part_name']
            get_num_list.append(b)
        get_apply_alter_list = models.apply_alter.objects.filter(status='失败').values('id','edit_remark','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','fail_reason','status_time')
        for c in get_apply_alter_list:
            id_rm = id_rm + 1
            c['id_rm']=id_rm
            c['type'] = 'alter'
            if c['user_identity']=='员工类别3':
                c['user_part']='员工类别3'
            else:
                c['user_part']=c['user_part__part_name']
            get_num_list.append(c)
        get_apply_del_list = models.apply_del.objects.filter(status='失败').values('id','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','fail_reason','status_time')
        for d in get_apply_del_list:
            id_rm = id_rm + 1
            d['id_rm']=id_rm
            d['edit_remark']='删除'
            d['type'] = 'del'
            if d['user_identity']=='员工类别3':
                d['user_part']='员工类别3'
            else:
                d['user_part']=d['user_part__part_name']
            get_num_list.append(d)
    count=len(get_num_list)
    paginator = Paginator(get_num_list, limit)
    loaded = paginator.page(page)
    loaded = list(loaded)
    data={"code":0,"msg":"","count":count,"data":loaded}
    return JsonResponse(data,safe=False)

#审批View End*********************************************************************************************************

#确认View *********************************************************************************************************
@superuser_only
@login_required(login_url='/login')
def apply_remark(request):#确认
    return render(request, 'manage_no/apply_remark.html')

@superuser_only
@login_required(login_url='/login')
def apply_upload(request):#上传回执
    if request.method == 'POST':  # 获取对象
        f = request.FILES['file']
        www=os.path.abspath('.')
        file_name = datetime.strftime(datetime.now(),'%Y%m%d%H%M%S')+'.xlsx'
        destination = open(os.path.join(www+"/upload_excel",file_name), 'wb+')
        for chunk in f.chunks():
            destination.write(chunk)
        destination.close()
        excel_tosql(www+"/upload_excel/"+file_name)
        return JsonResponse({"code":0},safe=False)

def excel_tosql(path):
    excel_data =get_excel_data(path)
    for i in excel_data:
        models.no_pool.objects.filter(short_number=i[1]).update(is_use='0')
        models.apply_add_main.objects.filter(Q(phone_number=i[0])&~Q(status='成功')).update(status='失败',status_time=datetime.now(),fail_reason=i[2])
        models.apply_add_vice.objects.filter(Q(phone_number=i[0])&~Q(status='成功')).update(status='失败',status_time=datetime.now(),fail_reason=i[2])
        models.apply_alter.objects.filter(Q(phone_number=i[0])&~Q(status='成功')).update(status='失败',status_time=datetime.now(),fail_reason=i[2])
        models.apply_del.objects.filter(Q(phone_number=i[0])&~Q(status='成功')).update(status='失败',status_time=datetime.now(),fail_reason=i[2])

def get_excel_data(path):#返回读取上传的数据
    data=[]
    workbook = xlrd.open_workbook(path)
    sheet1 = workbook.sheet_by_index(0)
    for i in range(sheet1.nrows):
        row = sheet1.row_values(i)
        if i >0:
            phone_num = row[0].split('|')[0]
            short_num = row[0].split('|')[1]
            fail_reason = row[1]
            line = [phone_num,short_num,fail_reason]
            data.append(line)
    return data

@superuser_only
@login_required(login_url='/login')
def apply_remark_data(request):#确认
    page = request.POST['page']
    limit = request.POST['limit']
    id_rm = 0
    get_num_list = []
    get_apply_addmain_list = models.apply_add_main.objects.filter(status__icontains='待办理').values('id','status','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
    for a in get_apply_addmain_list:
        id_rm = id_rm + 1
        a['id_rm']=id_rm
        a['type'] = 'addmain'
        if a['user_identity']=='员工类别3':
            a['user_part']='员工类别3'
        else:
            a['user_part']=a['user_part__part_name']
        if "补单" in a['status']:
            a['edit_remark']='主号新增(补单)'
        else:
            a['edit_remark']='主号新增'
        get_num_list.append(a)
    get_apply_addvice_list = models.apply_add_vice.objects.filter(status__icontains='待办理').values('id','status','user_name','id_main__user_identity','phone_number','short_number','id_main__user_company','id_main__user_part__part_name','status_time')
    for b in get_apply_addvice_list:
        id_rm = id_rm + 1
        b['id_rm']=id_rm
        b['type'] = 'addvice'
        b['user_company'] = b['id_main__user_company']
        if b['id_main__user_identity']=='员工类别3':
            b['user_part']='员工类别3'
        else:
            b['user_part']=b['id_main__user_part__part_name']
        if "补单" in b['status']:
            b['edit_remark']='副号新增(补单)'
        else:
            b['edit_remark']='副号新增'
        get_num_list.append(b)
    get_apply_alter_list = models.apply_alter.objects.filter(status__icontains='待办理').values('id','status','edit_remark','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
    for c in get_apply_alter_list:
        id_rm = id_rm + 1
        c['id_rm']=id_rm
        c['type'] = 'alter'
        if c['user_identity']=='员工类别3':
            c['user_part']='员工类别3'
        else:
            c['user_part']=c['user_part__part_name']
        if "补单" in c['status']:
            c['edit_remark']=c['edit_remark']+'(补单)'
        get_num_list.append(c)
    get_apply_del_list = models.apply_del.objects.filter(status__icontains='待办理').values('number_id','number_type','id','status','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
    for d in get_apply_del_list:
        id_rm = id_rm + 1
        d['id_rm']=id_rm
        d['edit_remark']='删除'
        d['type'] = 'del'
        if d['user_identity']=='员工类别3':
            d['user_part']='员工类别3'
        else:
            d['user_part']=d['user_part__part_name']
        if "补单" in d['status']:
            d['edit_remark']='删除(补单)'
        else:
            d['edit_remark']='删除'
        get_num_list.append(d)
        if d['number_type']=='main':
            get_vice_list = models.number_vice.objects.filter(id_main=d['number_id']).values('id','user_name','id_card','id_main__user_identity',
                'phone_number','short_number','id_main__user_company','id_main__user_part__part_name','id_main__user_name')
            for i in get_vice_list:
                id_rm = id_rm + 1
                i['id_rm']=id_rm
                i['edit_remark']='删除'
                i['user_company']=i['id_main__user_company']
                i['user_part']=i['id_main__user_part__part_name']
                i['status_time']='主号删除、副号自动删除'
                get_num_list.append(i)
    count=len(get_num_list)
    paginator = Paginator(get_num_list, limit)
    loaded = paginator.page(page)
    loaded = list(loaded)
    data={"code":0,"msg":"","count":count,"data":loaded}
    return JsonResponse(data,safe=False)

@superuser_only
@login_required(login_url='/login')
def apply_remark_is(request):#确认通过
    data = json.loads(request.POST['remark_data'])
    remark_time =  datetime.now()
    for i in data:
        if '新增' in i['edit_remark']:
            if i['type']=='addmain':
                models.apply_add_main.objects.filter(id=i['id']).update(status='待回执',status_time=datetime.now())
            else:
                models.apply_add_vice.objects.filter(id=i['id']).update(status='待回执',status_time=datetime.now())
        elif  '变更' in i['edit_remark']:
            models.apply_alter.objects.filter(id=i['id']).update(status='待回执',status_time=datetime.now())
        elif '删除' in i['edit_remark']:
            models.apply_del.objects.filter(id=i['id']).update(status='待回执',status_time=datetime.now())
    doc_path = str(os.getcwd())+'/templates/remake.docx'
    tpl = DocxTemplate(doc_path)
    name = datetime.now().strftime('%Y%m%d%H%M%S')
    now_time = datetime.now().strftime('%Y-%m-%d')
    filename=str(os.getcwd())+'/word_out/'+name+'.docx'
    context = {
        'now_time':now_time,
        'alerts':data
    }
    tpl.render(context)
    tpl.save(filename)
    data={"status":"success","msg":"反馈成功","filePath":name}
    return JsonResponse(data,safe=False)

@superuser_only
@login_required(login_url='/login')
def apply_remark_down(request,post_id):#下载
    filename=str(os.getcwd())+'/word_out/'+post_id+'.docx'
    file = open(filename, 'rb')
    response = FileResponse(file)
    down_name = 'attachment;filename=XX小号登记表.docx'
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = down_name.encode('utf-8','ISO-8859-1')
    return response

#确认View End ********************************************************************************************************
#回执View  ********************************************************************************************************
@login_required(login_url='/login')
def apply_hz(request):#确认
    user = request.user
    id_rm = 0
    get_num_list = []
    if user.is_superuser==1:
        get_apply_addmain_list = models.apply_add_main.objects.filter(status='待回执').values('id','status','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
        for a in get_apply_addmain_list:
            id_rm = id_rm + 1
            a['id_rm']=id_rm
            a['type'] = 'addmain'
            a['edit_remark']='主号新增'
            if a['user_identity']=='员工类别3':
                a['user_part']='员工类别3'
            else:
                a['user_part']=a['user_part__part_name']
            get_num_list.append(a)
        get_apply_addvice_list = models.apply_add_vice.objects.filter(status='待回执').values('id','status','user_name','id_main__user_identity','phone_number','short_number','id_main__user_company','id_main__user_part__part_name','status_time')
        for b in get_apply_addvice_list:
            id_rm = id_rm + 1
            b['id_rm']=id_rm
            b['edit_remark']='副号新增'
            b['type'] = 'addvice'
            b['user_company'] = b['id_main__user_company']
            if b['id_main__user_identity']=='员工类别3':
                b['user_part']='员工类别3'
            else:
                b['user_part']=b['id_main__user_part__part_name']
            get_num_list.append(b)
        get_apply_alter_list = models.apply_alter.objects.filter(status='待回执').values('id','status','edit_remark','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
        for c in get_apply_alter_list:
            id_rm = id_rm + 1
            c['id_rm']=id_rm
            c['type'] = 'alter'
            if c['user_identity']=='员工类别3':
                c['user_part']='员工类别3'
            else:
                c['user_part']=c['user_part__part_name']
            get_num_list.append(c)
        get_apply_del_list = models.apply_del.objects.filter(status='待回执').values('id','status','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
        for d in get_apply_del_list:
            id_rm = id_rm + 1
            d['id_rm']=id_rm
            d['edit_remark']='删除'
            d['type'] = 'del'
            if d['user_identity']=='员工类别3':
                d['user_part']='员工类别3'
            else:
                d['user_part']=d['user_part__part_name']
            get_num_list.append(d)
    else:
        if user.is_staff==0:
            get_apply_addmain_list = models.apply_add_main.objects.filter(status='待回执',user_company=user.user_company).values('id','status','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
            for a in get_apply_addmain_list:
                id_rm = id_rm + 1
                a['id_rm']=id_rm
                a['type'] = 'addmain'
                a['edit_remark']='主号新增'
                if a['user_identity']=='员工类别3':
                    a['user_part']='员工类别3'
                else:
                    a['user_part']=a['user_part__part_name']
                get_num_list.append(a)
            get_apply_addvice_list = models.apply_add_vice.objects.filter(status='待回执',id_main__user_company=user.user_company).values('id','status','user_name','id_main__user_identity','phone_number','short_number','id_main__user_company','id_main__user_part__part_name','status_time')
            for b in get_apply_addvice_list:
                id_rm = id_rm + 1
                b['id_rm']=id_rm
                b['edit_remark']='副号新增'
                b['type'] = 'addvice'
                b['user_company'] = b['id_main__user_company']
                if b['id_main__user_identity']=='员工类别3':
                    b['user_part']='员工类别3'
                else:
                    b['user_part']=b['id_main__user_part__part_name']
                get_num_list.append(b)
            get_apply_alter_list = models.apply_alter.objects.filter(status='待回执',user_company=user.user_company).values('id','status','edit_remark','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
            for c in get_apply_alter_list:
                id_rm = id_rm + 1
                c['id_rm']=id_rm
                c['type'] = 'alter'
                if c['user_identity']=='员工类别3':
                    c['user_part']='员工类别3'
                else:
                    c['user_part']=c['user_part__part_name']
                get_num_list.append(c)
            get_apply_del_list = models.apply_del.objects.filter(status='待回执',user_company=user.user_company).values('id','status','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
            for d in get_apply_del_list:
                id_rm = id_rm + 1
                d['id_rm']=id_rm
                d['edit_remark']='删除'
                d['type'] = 'del'
                if d['user_identity']=='员工类别3':
                    d['user_part']='员工类别3'
                else:
                    d['user_part']=d['user_part__part_name']
                get_num_list.append(d)
        else:
            get_apply_addmain_list = models.apply_add_main.objects.filter(status='待回执',user_part=user.user_part).values('id','status','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
            for a in get_apply_addmain_list:
                id_rm = id_rm + 1
                a['id_rm']=id_rm
                a['type'] = 'addmain'
                if a['user_identity']=='员工类别3':
                    a['user_part']='员工类别3'
                else:
                    a['user_part']=a['user_part__part_name']
                if "补单" in a['status']:
                    a['edit_remark']='主号新增(补单)'
                else:
                    a['edit_remark']='主号新增'
                get_num_list.append(a)
            get_apply_addvice_list = models.apply_add_vice.objects.filter(status='待回执',id_main__user_part=user.user_part).values('id','status','user_name','id_main__user_identity','phone_number','short_number','id_main__user_company','id_main__user_part__part_name','status_time')
            for b in get_apply_addvice_list:
                id_rm = id_rm + 1
                b['id_rm']=id_rm
                b['edit_remark']='副号新增'
                b['type'] = 'addvice'
                b['user_company'] = b['id_main__user_company']
                if b['id_main__user_identity']=='员工类别3':
                    b['user_part']='员工类别3'
                else:
                    b['user_part']=b['id_main__user_part__part_name']
                get_num_list.append(b)
            get_apply_alter_list = models.apply_alter.objects.filter(status='待回执',user_part=user.user_part).values('id','status','edit_remark','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
            for c in get_apply_alter_list:
                id_rm = id_rm + 1
                c['id_rm']=id_rm
                c['type'] = 'alter'
                if c['user_identity']=='员工类别3':
                    c['user_part']='员工类别3'
                else:
                    c['user_part']=c['user_part__part_name']
                get_num_list.append(c)
            get_apply_del_list = models.apply_del.objects.filter(status='待回执',user_part=user.user_part).values('id','status','user_name','user_identity','user_company','user_part__part_name','phone_number','short_number','status_time')
            for d in get_apply_del_list:
                id_rm = id_rm + 1
                d['id_rm']=id_rm
                d['edit_remark']='删除'
                d['type'] = 'del'
                if d['user_identity']=='员工类别3':
                    d['user_part']='员工类别3'
                else:
                    d['user_part']=d['user_part__part_name']
                get_num_list.append(d)
    context = {
    'ContentInfo': get_num_list,
    }
    return render(request, 'manage_no/apply_hz.html', context)

@login_required(login_url='/login')
def hz_success(request):#回执完成
    hz_data = request.POST['hz_data']
    hz_list=hz_data.split('_')
    hz_id=hz_list[0]
    hz_type=hz_list[1]
    if hz_type=='addmain':
        models.apply_add_main.objects.filter(id=hz_id).update(status='已完成',status_time=datetime.now())
        get_num_list = models.apply_add_main.objects.filter(id=hz_id).values()
        get_num_dict= get_num_list[0]
        p = Pinyin()
        username =  get_num_dict['user_name']
        firest_py = p.get_initial(username[0])
        models.number_main.objects.create(user_name=get_num_dict['user_name'],id_card=get_num_dict['id_card'],id_work=get_num_dict['id_work'],
        user_company=get_num_dict['user_company'],user_part_id=get_num_dict['user_part_id'],user_identity=get_num_dict['user_identity'],
        user_position=get_num_dict['user_position'],phone_number=get_num_dict['phone_number'],short_number=get_num_dict['short_number'],
        remark=get_num_dict['remark'],is_app='1',pinyin=firest_py)
    elif hz_type=='addvice':
        models.apply_add_vice.objects.filter(id=hz_id).update(status='已完成',status_time=datetime.now())
        get_num_list = models.apply_add_vice.objects.filter(id=hz_id).values()
        get_num_dict= get_num_list[0]
        id_main = models.number_main.objects.get(id=get_num_dict['id_main_id'])
        models.number_vice.objects.create(id_main=id_main,user_name=get_num_dict['user_name'],id_card=get_num_dict['id_card'],phone_number=get_num_dict['phone_number'],short_number=get_num_dict['short_number'],remark=get_num_dict['remark'])
    elif hz_type=='alter':
        models.apply_alter.objects.filter(id=hz_id).update(status='已完成',status_time=datetime.now())
        get_num_list = models.apply_alter.objects.filter(id=hz_id).values()
        get_num_dict= get_num_list[0]
        if get_num_dict['edit_type'] == '1':
            if get_num_dict['number_type'] == 'main':
                models.number_main.objects.filter(id=get_num_dict['number_id']).update(phone_number=get_num_dict['phone_number'])
            else:
                models.number_vice.objects.filter(id=get_num_dict['number_id']).update(phone_number=get_num_dict['phone_number'])
        else:
            if get_num_dict['number_type'] == 'main':
                models.number_main.objects.filter(id=get_num_dict['number_id']).update(short_number=get_num_dict['short_number'])
            else:
                models.number_vice.objects.filter(id=get_num_dict['number_id']).update(short_number=get_num_dict['short_number'])
            short_number_old=get_num_dict['edit_remark']
            models.no_pool.objects.filter(short_number=short_number_old[6:12]).update(is_use='0')
    elif hz_type=='del':
        models.apply_del.objects.filter(id=hz_id).update(status='已完成',status_time=datetime.now())
        get_num_list = models.apply_del.objects.filter(id=hz_id).values()
        get_num_dict= get_num_list[0]
        if get_num_dict['number_type'] == 'main':
            get_vice_list = models.number_vice.objects.filter(id_main=get_num_dict['number_id']).values()
            if len(get_vice_list)==0:
                models.number_main.objects.filter(id=get_num_dict['number_id']).delete()
            else:
                for i in get_vice_list:
                    models.number_vice.objects.filter(id=i['id']).delete()
                models.number_main.objects.filter(id=get_num_dict['number_id']).delete()
        else:
            models.number_vice.objects.filter(id=get_num_dict['number_id']).delete()
    return HttpResponse('{"status":"success","msg":"回执成功"}',content_type='application/json')

@login_required(login_url='/login')
def hz_fail(request):#回执完成
    hz_data = request.POST['hz_data']
    hz_list=hz_data.split('_')
    hz_id=hz_list[0]
    hz_type=hz_list[1]
    if hz_type=='addmain':
        models.apply_add_main.objects.filter(id=hz_id).update(status='失败',status_time=datetime.now())
        get_num_list = models.apply_add_main.objects.filter(id=hz_id).values()
        get_num_dict= get_num_list[0]
        models.no_pool.objects.filter(short_number=get_num_dict['short_number']).update(is_use='0')
    elif hz_type=='addvice':
        models.apply_add_vice.objects.filter(id=hz_id).update(status='失败',status_time=datetime.now())
        get_num_list = models.apply_add_vice.objects.filter(id=hz_id).values()
        get_num_dict= get_num_list[0]
        models.no_pool.objects.filter(short_number=get_num_dict['short_number']).update(is_use='0')
    elif hz_type=='alter':
        models.apply_alter.objects.filter(id=hz_id).update(status='失败',status_time=datetime.now())
        get_num_list = models.apply_alter.objects.filter(id=hz_id).values()
        get_num_dict= get_num_list[0]
        if get_num_dict['edit_type'] == '1':
            models.no_pool.objects.filter(short_number=short_number_old[6:12]).update(is_use='0')
        else:
            short_number_old=get_num_dict['edit_remark']
            models.no_pool.objects.filter(short_number=short_number_old[6:12]).update(is_use='0')
    elif hz_type=='del':
        models.apply_del.objects.filter(id=hz_id).update(status='失败',status_time=datetime.now())
        get_num_list = models.apply_del.objects.filter(id=hz_id).values()
        get_num_dict= get_num_list[0]
        models.no_pool.objects.filter(short_number=get_num_dict['short_number']).update(is_use='0')
    return HttpResponse('{"status":"success","msg":"回执成功"}',content_type='application/json')
#回执View End ********************************************************************************************************
